Skip to content

Instantly share code, notes, and snippets.

@chrisshroba chrisshroba/regex
Last active Dec 16, 2016

Embed
What would you like to do?
A CLI for searching for a matching group and returning only that group (Rather than the entire matching line as grep does)
#!/usr/bin/env python
import re
from sys import argv
if len(argv)<3:
print("Usage:")
print("{} <string> <pattern>".format(argv[0]))
exit(2)
s = argv[1]
pattern = argv[2]
lines = s.split('\n')
for line in lines:
match = re.search(pattern, line)
if match is not None:
result = match.groups()[0]
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.