Reputation: 271
Here is a pattern object that I tried to make in python using the re module. What I am going for is something that will take the string "(\exists x)(Px*Qx)" and find just the "Px*Qx" portion. I thought that I would try to use the lookahead and lookbehind assertions. I'm not sure if I am using this wrong or if there is something wrong with the ( character.
p = re.compile(r'?<=[(]\w+?=[)]')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/re.py", line 190, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.7/re.py", line 244, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat
Upvotes: 4
Views: 2384
Reputation: 9168
You need to put tokens into a group to apply lookbehind (add parentheses around): (?<=[(]\w+(?=[)]))
Python doesn't support variable repetition inside lookbehind. So, you can not write \w+
there.
A regex with only a lookbehind matches nothing! Lookbehind means "Try to match this thing, then stay at the same starting position to match following part of the regex." But, in your case, no following part. So, nothing matches.
If you want to obtain text inside the last parentheses:
^.*\((.*?)\)$
If you want to skip first parentheses and obtain remaining part removing parentheses:
^\(.*?\)\((.*)\)$
Please define what you want more concretely, so I can help you to write a proper regex.
Upvotes: 3