Regular Expressions
This page is under construction. Please come back later.
#!/usr/bin/env python3; import Utils import re # Begin Main s = "Four score and seven years ago..." print("Match 1: " + str(Utils.defined(re.compile(r's.*e').search(s)))) print("Match 2: " + str(Utils.defined(re.compile(r'\bs.*e\b').search(s)))) print("Match 3: " + str(Utils.defined(re.compile(r's...e').search(s)))) print("Match 4: " + str(Utils.defined(re.compile(r'b.d').search(s)))) subgroups = Utils.findFirst(re.compile(r'(\w+)\s*(\w+)'), s) print("Find First (with subgroups): " + Utils.listToString(subgroups)) subgroups = Utils.findFirst(re.compile(r'bad match'), s) print("Find First (bad match): " + Utils.listToString(subgroups)) matches = re.compile(r'\w+').findall(s) print("Find All: (matches only)" + Utils.listToString(matches)) matches = re.compile(r'bad match').findall(s) print("Find All (bad match): " + Utils.listToString(matches))
Output
$ python3 RegEx1.py
Match 1: True
Match 2: True
Match 3: True
Match 4: False
Find First (with subgroups): ["Four score", "Four", "score"]
Find First (bad match): []
Find All: (matches only)["Four", "score", "and", "seven", "years", "ago"]
Find All (bad match): []
#!/usr/bin/env python3; import Utils import re # Begin Main s = "Four score and seven years ago..." print("Replace First 1: " + re.compile(r'\.\.\.').sub("!!!", s, 1)) print("Replace First 2: " + re.compile(r'\b...\b').sub("???", s, 1)) print("Replace First 3: " + re.compile(r'b.d').sub("???", s, 1)) print("Replace First 4: " + re.compile(r'(\w+) (\w+)').sub(re.compile(r'\2 \1'), s, 1)) print("Replace All 1: " + re.compile(r'\b...\b').sub("???", s)) print("Replace All 2: " + re.compile(r'(\w+) (\w+)').sub(re.compile(r'\2 \1'), s))
Output
$ python3 RegEx2.py
Replace First 1: Four score and seven years ago!!!
Replace First 2: Four score ??? seven years ago...
Replace First 3: Four score and seven years ago...
Traceback (most recent call last):
File "/Users/rich/Desktop/Resources/pureprogrammer/py/examples/RegEx2.py", line 10, in <module>
print("Replace First 4: " + re.compile(r'(\w+) (\w+)').sub(re.compile(r'\2 \1'), s, 1))
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/re.py", line 252, in compile
return _compile(pattern, flags)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/re.py", line 304, in _compile
p = sre_compile.compile(pattern, flags)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/sre_parse.py", line 948, in parse
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/sre_parse.py", line 443, in _parse_sub
itemsappend(_parse(source, state, verbose, nested + 1,
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/sre_parse.py", line 525, in _parse
code = _escape(source, this, state)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/sre_parse.py", line 423, in _escape
raise source.error("invalid group reference %d" % group, len(escape) - 1)
re.error: invalid group reference 2 at position 1
#!/usr/bin/env python3; import Utils import re # Begin Main str = "Four score and seven years ago..." print("Split 1: " + Utils.listToString(re.compile(r' ').split(str))) print("Split 2: " + Utils.listToString(re.compile(r'[eo]').split(str))) print("Split 3: " + Utils.listToString(re.compile(r'\s').split(str))) print("Split 4: " + Utils.listToString(re.compile(r'\W').split(str)))
Output
$ python3 RegEx3.py
Split 1: ["Four", "score", "and", "seven", "years", "ago..."]
Split 2: ["F", "ur sc", "r", " and s", "v", "n y", "ars ag", "..."]
Split 3: ["Four", "score", "and", "seven", "years", "ago..."]
Split 4: ["Four", "score", "and", "seven", "years", "ago", "", "", ""]
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Python Language Reference]]
- [[Python Standard Library]]
- [[Python at TutorialsPoint]]