469,929 Members | 1,723 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

local greediness ???

hi, all. I need to process a file with the following format:
$ cat sample
[(some text)2.3(more text)4.5(more text here)]
[(aa bb ccc)-1.2(kdk)12.0(xxxyyy)]
[(xxx)11.0(bbb\))8.9(end here)]
........

my goal here is for each line, extract every '(.*)' (including the
round
brackets, put them in a list, and extract every float on the same line
and put them in a list.. here is my code:

p = re.compile(r'\[.*\]$')
num = re.compile(r'[-\d]+[.\d]*')
brac = re.compile(r'\(.*?\)')

for line in ifp:
if p.match(line):
x = num.findall(line)
y = brac.findall(line)
print x, y len(x), len(y)

Now, this works for most of the lines. however, I'm having problems
with
lines such as line 3 above (in the sample file). here, (bbb\)) contains
an escaped
')' and the re I use will match it (because of the non-greedy '?'). But
I want this to
be ignored since it's escaped. is there a such thing as local
greediness??
Can anyone suggest a way to deal with this here..
thanks.

Apr 19 '06 #1
3 1107
On 19/04/2006 3:09 PM, ty****@gmail.com wrote:
hi, all. I need to process a file with the following format:
$ cat sample
[(some text)2.3(more text)4.5(more text here)]
[(aa bb ccc)-1.2(kdk)12.0(xxxyyy)]
[(xxx)11.0(bbb\))8.9(end here)]
.......

my goal here is for each line, extract every '(.*)' (including the
round
brackets, put them in a list, and extract every float on the same line
and put them in a list.. here is my code:

p = re.compile(r'\[.*\]$')
num = re.compile(r'[-\d]+[.\d]*')
brac = re.compile(r'\(.*?\)')

for line in ifp:
if p.match(line):
x = num.findall(line)
y = brac.findall(line)
print x, y len(x), len(y)

Now, this works for most of the lines. however, I'm having problems
with
lines such as line 3 above (in the sample file). here, (bbb\)) contains
an escaped
')' and the re I use will match it (because of the non-greedy '?'). But
I want this to
be ignored since it's escaped. is there a such thing as local
greediness??
Can anyone suggest a way to deal with this here..
thanks.


For a start, your brac pattern is better rewritten to avoid the
non-greedy ? tag: r'\([^)]*\)' -- this says the middle part is zero or
more occurrences of a single character that is not a ')'

To handle the pesky backslash-as-escape, we need to extend that to: zero
or more occurrences of either (a) a single character that is not a ')'
or (b) the two-character string r"\)". This gives us something like this:

#>>> brac = re.compile(r'\((?:\\\)|[^)])*\)')
#>>> tests = r"(xxx)123.4(bbb\))5.6(end\Zhere)7.8()9.0(\))1.2(a b\)cd)"
#>>> brac.findall(tests)
['(xxx)', '(bbb\\))', '(end\\Zhere)', '()', '(\\))', '(ab\\)cd)']
#>>>

Pretty, isn't it? Maybe better done with a hand-coded state machine.
Apr 19 '06 #2
How about using the numbers as delimiters:
pat = re.compile(r"[\d\.\-]+")
pat.split("[(some text)2.3(more text)4.5(more text here)]") ['[(some text)', '(more text)', '(more text here)]'] pat.findall("[(some text)2.3(more text)4.5(more text here)]") ['2.3', '4.5'] pat.split("[(xxx)11.0(bbb\))8.9(end here)] ") ['[(xxx)', '(bbb\\))', '(end here)] '] pat.findall("[(xxx)11.0(bbb\))8.9(end here)] ")
['11.0', '8.9']

ty****@gmail.com wrote: hi, all. I need to process a file with the following format:
$ cat sample
[(some text)2.3(more text)4.5(more text here)]
[(aa bb ccc)-1.2(kdk)12.0(xxxyyy)]
[(xxx)11.0(bbb\))8.9(end here)]
.......

my goal here is for each line, extract every '(.*)' (including the
round
brackets, put them in a list, and extract every float on the same line
and put them in a list.. here is my code:

p = re.compile(r'\[.*\]$')
num = re.compile(r'[-\d]+[.\d]*')
brac = re.compile(r'\(.*?\)')

for line in ifp:
if p.match(line):
x = num.findall(line)
y = brac.findall(line)
print x, y len(x), len(y)

Now, this works for most of the lines. however, I'm having problems
with
lines such as line 3 above (in the sample file). here, (bbb\)) contains
an escaped
')' and the re I use will match it (because of the non-greedy '?'). But
I want this to
be ignored since it's escaped. is there a such thing as local
greediness??
Can anyone suggest a way to deal with this here..
thanks.


Apr 19 '06 #3
<ty****@gmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
hi, all. I need to process a file with the following format:
$ cat sample
[(some text)2.3(more text)4.5(more text here)]
[(aa bb ccc)-1.2(kdk)12.0(xxxyyy)]
[(xxx)11.0(bbb\))8.9(end here)]
.......

my goal here is for each line, extract every '(.*)' (including the
round
brackets, put them in a list, and extract every float on the same line
and put them in a list..


Are you wedded to re's? Here's a pyparsing approach for your perusal. It
uses the new QuotedString class, treating your ()-enclosed elements as
custom quoted strings (including backslash escape support).

Some other things the parser does for you during parsing:
- converts the numeric strings to floats
- processes the \) escaped paren, returning just the )
Why not? While parsing, the parser "knows" it has just parsed a floating
point number (or an escaped character), go ahead and do the conversion too.

-- Paul
(Download pyparsing at http://pyparsing.sourceforge.net.)

--------------------
test = r"""
[(some text)2.3(more text)4.5(more text here)]
[(aa bb ccc)-1.2(kdk)12.0(xxxyyy)]
[(xxx)11.0(bbb\))8.9(end here)]
"""
from pyparsing import oneOf,Combine,Optional,Word,nums,QuotedString,Supp ress

# define a floating point number
sign = oneOf("+ -")
floatNum = Combine( Optional(sign) + Word(nums) + "." + Word(nums) )

# have parser convert to actual floats while parsing
floatNum.setParseAction(lambda s,l,t: float(t[0]))

# define a "quoted string" where ()'s are the opening and closing quotes
parenString = QuotedString("(",endQuoteChar=")",escChar="\\")

# define the overall entry structure
entry = Suppress("[") + parenString + floatNum + parenString + floatNum +
parenString + Suppress("]")

# scan for floats
for toks,start,end in floatNum.scanString(test):
print toks[0]
print

# scan for paren strings
for toks,start,end in parenString.scanString(test):
print toks[0]
print

# scan for entries
for toks,start,end in entry.scanString(test):
print toks
print
--------------------
Gives:
2.3
4.5
-1.2
12.0
11.0
8.9

some text
more text
more text here
aa bb ccc
kdk
xxxyyy
xxx
bbb)
end here

['some text', 2.2999999999999998, 'more text', 4.5, 'more text here']
['aa bb ccc', -1.2, 'kdk', 12.0, 'xxxyyy']
['xxx', 11.0, 'bbb)', 8.9000000000000004, 'end here']

Apr 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Steffen | last post: by
9 posts views Thread by Stefan Turalski \(stic\) | last post: by
23 posts views Thread by Timothy Madden | last post: by
28 posts views Thread by cpluslearn | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.