alvarofpp / mre

Maker Regular Expressions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lookahead in Group

alvarofpp opened this issue · comments

Lookahead assertion
(?=...)

from mre import Regex, Group

# Example of how it can work 1
print(Regex("Isaac", Group(" Asimov", lookahead=True)))

# Output: Isaac(?= Asimov)
# Regex without lookahead: Isaac( Asimov)

lookahead-positive

Negative lookahead assertion
(?!...)

from mre import Regex, Group

# Example of how it can work 1
print(Regex("Isaac", Group(" Asimov", lookahead=False)))

# Output: Isaac(?! Asimov)
# Regex without lookahead: Isaac( Asimov)

lookahead-negative

Note: the default value of the lookahead parameter is None.

Hi,

I'd like to take on this one :-)

I would say there's something wrong here in mixing Group with lookahead (and lookbehind for that mather). They are different things. Looking around in a regex is a non-capturing operation and has a different semantic than grouping although in uses the same symbol to start. I would recommend having a separated class for that.

@sgaist

I agree with you. I really didn’t try to do that. The classes could be created:

  • LookaheadGroup (or just Lookahead);
  • LookbehindGroup (or just Lookbehind).

Both could inherit from Group, passing non_capturing=False in the parent class's constructor.

#7