Re: Confused about 'positive lookbehind assertion'
"Andrew Durdin" <[EMAIL PROTECTED]> wrote: > On 9/25/07, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: >> >> Any idea what this positive lookbehind achieves which can't be done >> without it. >> I remember cases where positive look-ahead is useful. >> >> In the above example, r.search('abcdef') does the job of ensuring >> 'def' is preceded by 'abc'. > > AFAICT the only benefit I can see is that the lookbehind isn't > captured, so (a) it's not included in match.group(n), and (b) > match.start(), match.end(), and match.span() return the offsets of the > bit you actually wanted to capture. It also makes a difference if you are searching for multiple patterns and the lookbehind overlaps the previous result: >>> re.findall('(?<=abc)(abc|def)', 'abcabcdef') ['abc', 'def'] I think though that its real use is not at the beginning of a pattern but in the middle: you might have a greedy pattern which matches too easily (possibly to create a group), and then a guard which looks behind. e.g. >>> re.findall('(\d*)((?<=1)a|(?<=2)b)', '111a 111b 222b') [('111', 'a'), ('222', 'b')] This pattern finds all numbers ending in 1 followed by 'a', or ending in 2 followed by 'b' and it returns groups containing the number and the trailing letter. You could write '\d*(1a|2b)' but that doesn't get you the correct groups. -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused about 'positive lookbehind assertion'
On 9/25/07, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: > > Any idea what this positive lookbehind achieves which can't be done > without it. > I remember cases where positive look-ahead is useful. > > In the above example, r.search('abcdef') does the job of ensuring > 'def' is preceded by 'abc'. AFAICT the only benefit I can see is that the lookbehind isn't captured, so (a) it's not included in match.group(n), and (b) match.start(), match.end(), and match.span() return the offsets of the bit you actually wanted to capture. OTOH, I often find the negative lookbehind (?!...) very useful indeed. Andrew -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused about 'positive lookbehind assertion'
On Sep 25, 8:01 am, Erik Jones <[EMAIL PROTECTED]> wrote: > On Sep 24, 2007, at 9:38 PM, Robert Dailey wrote: > > > Hi, > > > I've been reading the python documentation on 'positive lookbehind > > assertion' and I don't understand at all how it works. The python > > docs give the following example: > > > " (?<=abc)def will find a match in "abcdef", since the lookbehind > > will back up 3 characters and check if the contained pattern matches." > > > Can anyone emphasize more on what this RE operation does? Thanks. > > Have you actually tried it out? > > >>> import re > >>> r = re.compile(r'(?<=abc)def') > >>> m1 = r.search('bcde') > >>> m1.group()'def' > 'def' > >>> m2 = r.search('bcdefff') > >>> m2 == None > True > > So, it matches 'def' but only if it is immediately preceded by 'abc'. Any idea what this positive lookbehind achieves which can't be done without it. I remember cases where positive look-ahead is useful. In the above example, r.search('abcdef') does the job of ensuring 'def' is preceded by 'abc'. Karthik > > Erik Jones > > Software Developer | Emma® > [EMAIL PROTECTED] > 800.595.4401 or 615.292.5888 > 615.292.0777 (fax) > > Emma helps organizations everywhere communicate & market in style. > Visit us online athttp://www.myemma.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused about 'positive lookbehind assertion'
On Sep 24, 2007, at 9:38 PM, Robert Dailey wrote: > Hi, > > I've been reading the python documentation on 'positive lookbehind > assertion' and I don't understand at all how it works. The python > docs give the following example: > > " (?<=abc)def will find a match in "abcdef", since the lookbehind > will back up 3 characters and check if the contained pattern matches." > > Can anyone emphasize more on what this RE operation does? Thanks. Have you actually tried it out? >>> import re >>> r = re.compile(r'(?<=abc)def') >>> m1 = r.search('bcde') >>> m1.group()'def' 'def' >>> m2 = r.search('bcdefff') >>> m2 == None True So, it matches 'def' but only if it is immediately preceded by 'abc'. Erik Jones Software Developer | Emma® [EMAIL PROTECTED] 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate & market in style. Visit us online at http://www.myemma.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused about 'positive lookbehind assertion'
I think I get it... it's really just a way (so it seems) to make characters get added to the found groups as they're matched. Thanks for your help. On 9/25/07, Andrew Durdin <[EMAIL PROTECTED]> wrote: > > On 9/25/07, Robert Dailey <[EMAIL PROTECTED]> wrote: > > Hi, > > > > I've been reading the python documentation on 'positive lookbehind > > assertion' and I don't understand at all how it works. The python docs > give > > the following example: > > > > " (?<=abc)def will find a match in "abcdef", since the lookbehind will > back > > up 3 characters and check if the contained pattern matches." > > > > Can anyone emphasize more on what this RE operation does? Thanks. > > It ensures that the regex will only match following the string in the > lookbehind group, but without capturing that string. > > As the docs say, "(?<=abc)def" will match "abcdef"; but it will not > match "def" (as the "abc" is not there). If it does match, the 0th > group in the match object will be "def". > > In contrast, the regex "abcdef" will also match "abcdef" and not > "def", but the 0th group will be "abcdef". > > The negative lookbehind is the opposite -- e.g. "(? match "def" but not "abcdef". > > Cheers, > > Andrew > -- http://mail.python.org/mailman/listinfo/python-list
re: Confused about 'positive lookbehind assertion'
Hi, I've been reading the python documentation on 'positive lookbehind assertion' and I don't understand at all how it works. The python docs give the following example: "**(?<=abc)def will find a match in "abcdef", since the lookbehind will back up 3 characters and check if the contained pattern matches." Can anyone emphasize more on what this RE operation does? Thanks. -- http://mail.python.org/mailman/listinfo/python-list