Paul McGuire pt...@austin.rr.com wrote:
On Apr 3, 9:26 pm, Paul Rubin http://phr...@nospam.invalid wrote:
bwgoudey bwgou...@gmail.com writes:
elif re.match(^DATASET:\s*(.+) , line):
m=re.match(^DATASET:\s*(.+) , line)
print m.group(1))
Sometimes I like to make a
On Apr 3, 9:26 pm, Paul Rubin http://phr...@nospam.invalid wrote:
bwgoudey bwgou...@gmail.com writes:
elif re.match(^DATASET:\s*(.+) , line):
m=re.match(^DATASET:\s*(.+) , line)
print m.group(1))
Sometimes I like to make a special class that saves the result:
class
(^DATASET:\s*(.+) , line)
print m.group(1))
which is ugly because of the duplication but I can't think of a nicer of way
of doing this that will allow for a lot of these sorts of cases. Any
suggestions?
--
View this message in context:
http://www.nabble.com/Best-way-to-extract-from-regex
in
context:http://www.nabble.com/Best-way-to-extract-from-regex-in-if-statement-...
Sent from the Python - python-list mailing list archive at Nabble.com.
How about something like:
your_regexes = [
re.compile('rx1'),
re.compile('rx2'),
# etc
]
for line in lines:
for rx
bwgoudey wrote:
I have a lot of if/elif cases based on regular expressions that I'm using to
filter stdin and using print to stdout. Often I want to print something
matched within the regular expression and the moment I've got a lot of cases
like:
...
elif re.match(^DATASET:\s*(.+) , line):
for a lot of these sorts of cases. Any
suggestions?
--
View this message in
context:http://www.nabble.com/Best-way-to-extract-from-regex-in-if-statement-...
Sent from the Python - python-list mailing list archive at Nabble.com.
How about something like:
your_regexes = [
re.compile('rx1
bwgoudey bwgou...@gmail.com writes:
elif re.match(^DATASET:\s*(.+) , line):
m=re.match(^DATASET:\s*(.+) , line)
print m.group(1))
Sometimes I like to make a special class that saves the result:
class Reg(object): # illustrative code, not tested
def match(self,
Or in case you want to handle each regexp differently, you can
construct a dict {regexp : callback_function} that picks the right
action depending on which regexp matched.
One word of caution: dicts are unsorted, so if more than one
regexp can match a given line, they either need to map to