By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,602 Members | 1,507 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,602 IT Pros & Developers. It's quick & easy.

Nested Regex Conditionals

P: n/a

Hi All,

I know that several of you will probably want to reply "you should write
a parser", and I may. For that matter any tips on theory in that
direction would be appreciated.

However, if you would indulge me in my regex question I would also be
most grateful.

I'm writing an edi parser and to be in compliance with the specification
I need to have conditionals that are dependent on conditionals. In some
regular expression implementations this is possible. The code ...

#!/usr/bin/env python
import re
pattern = re.compile(r"""
(?P<first>(first))
(?(first)
(?P<second>(second))
)
(?(second)
(?P<third>(third))
)
""", re.VERBOSE)
string = 'firstsecondthird'
match = re.match(pattern,string)
print match.group('first','second','third')

Prints ('first', 'second', None)

and I haven't found any way to have a second conditional, nor any
reference to it in any documentation I've found.

Am I missing something, and it is possible? Or is it not possible in python?

It seems like it might be a bug, as it knows there is a group (None,
instead of an IndexError), but it doesn't match ...

Thanks for any help :)

Paul
Aug 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
This works (note relocated right paren at (?(second) ):

#!/usr/bin/env python
import re
original_pattern = re.compile(r"""
(?P<first>(first))
(?(first)
(?P<second>(second))
)
(?(second)
(?P<third>(third))
)
""", re.VERBOSE)
pattern = re.compile(r"""
(?P<first>(first))
(?(first)
(?P<second>(second))
)
(?(second))
(?P<third>(third))
""", re.VERBOSE)

string = 'firstsecondthird'
match = re.match(pattern,string)
print match.group('first','second','third')
Prints out:
('first', 'second', 'third')
The pyparsing alternative looks like:

first = Literal("first").setResultsName("first")
second = Literal("second").setResultsName("second")
third = Literal("third").setResultsName("third")

# define line
line = first + second + third

print line.parseString("firstsecondthird")

Prints out:
['first', 'second', 'third']

-- Paul

Aug 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.