Skip to content

Instantly share code, notes, and snippets.

@JohnL4
Last active July 13, 2020 16:49
Show Gist options
  • Select an option

  • Save JohnL4/2cfbc7d4a31ed67c857adac1d8909af0 to your computer and use it in GitHub Desktop.

Select an option

Save JohnL4/2cfbc7d4a31ed67c857adac1d8909af0 to your computer and use it in GitHub Desktop.
Capture regexp subexpressions with python. Better than Perl.
>>> 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