473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Nested Regex Conditionals


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
1 1967
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dean H. Saxe | last post by:
I'm currently developing a tool in perl to search out potential XSS (Cross Site Scripting) vulnerabilities and correct them in a ColdFusion based web app. I've been having great success so far,...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
2
by: Matt T. | last post by:
I am trying to replace the nested table tag in the follow string using a regular expression, but I am not having any success. I am new at using regular expressions, so I am sure I am just...
1
by: howard dierking | last post by:
Hi all, I'm having a problem with a reg ex. Essentiall, I'm trying to isolate variable declarations from old vbscript where there was no explicit declaration requirement. This should seem easy...
2
by: Surfdog | last post by:
Hello all, I'm unable to figure out how to process the zip codes with regexes. We have a custom component that takes a find pattern, a replace pattern, and the input value, and will output the...
3
by: Ward Bekker | last post by:
Hi, I need a regular expression that will match only the tags that have nested tags inside them: Input: <control id=1><control id=2></control></control><control id=3></control> Goal:
4
by: Allen | last post by:
For my web-based php regex find/replace do-hickey, I need to match individual back references and wrap a tag around them so they'll be unique to the rest of the match for individual color markup. ...
0
by: Fabio R. | last post by:
I'm going crazy... Can someone help me to do a regex to search for something like nested if? I have these tags: <if>...<else>...<endif> that can be nested: <if> <if>...<else>...<endif>
4
by: MooMaster | last post by:
I'm trying to develop a little script that does some string manipulation. I have some few hundred strings that currently look like this: cond(a,b,c) and I want them to look like this: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.