Last active
July 13, 2020 16:49
-
-
Save JohnL4/2cfbc7d4a31ed67c857adac1d8909af0 to your computer and use it in GitHub Desktop.
Capture regexp subexpressions with python. Better than Perl.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> import re | |
| >>> m = re.match( r"-*(a+)-*(b+)-*(c+)", "---aaa---bbb--ccc---") | |
| >>> m[1] | |
| 'aaa' | |
| >>> m[3] | |
| 'ccc' | |
| >>> m.group(2,3) | |
| ('bbb', 'ccc') | |
| See python fileinput for looping over lines of a series of files on stdin. | |
| import fileinput | |
| for line in fileinput.input(): | |
| line = line.rstrip('\r\n') # Chomp. | |
| process( line) | |
| print( line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment