local greediness ???

johnzenger at gmail.com johnzenger at gmail.com
Wed Apr 19 04:22:13 EDT 2006


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']

tygerc at 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.




More information about the Python-list mailing list