d:\Temp>py
Python 3.13.5 (tags/v3.13.5:6cb20a2, Jun 11 2025, 16:15:46) [MSC v.1943 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> text = '"Tarzan and Tarzan" and "Tarzan" and Tarzan'
>>> ptn1 = r'(?>(?<=(")|))Tarzan(?(1)(?!"))'
>>> regexp1 = re.compile(ptn1)
Traceback (most recent call last):
File "<python-input-7>", line 1, in <module>
regexp1 = re.compile(ptn1)
...
...
File "C:\Users\jfong\AppData\Local\Programs\Python\Python313\Lib\re\_compiler.py", line 153, in _compile
raise PatternError("look-behind requires fixed-width pattern")
re.PatternError: look-behind requires fixed-width pattern
If I make a minor modification in ptn1 by adding one space following the ‘|’. It passes the compile and has the result.
When you use findall, re returns the groups if any are defined or the entire match if no groups are defined.
It looks like whatever methods or functions you’re using in PCRE and C++Builder are returning the same as [m.group() for m in re.finditer(ptn2, text)] would using the re module.
So, none are incorrect.
The re module doesn’t support variable-wiodth lookbehinds, but the regex module on PyPI does.
Yes, you are right, the following code has the expected result,
>>> for m in regexp2.finditer(text):
... print(m)
...
<re.Match object; span=(1, 7), match='Tarzan'>
<re.Match object; span=(12, 18), match='Tarzan'>
<re.Match object; span=(37, 43), match='Tarzan'>
>>>
There is one thing still confuse me. Why an empty string follows the ‘|’ will cause the error
re.PatternError: look-behind requires fixed-width pattern
Isn’t an empty string has fixed-length 0?
Your lookbehind is (?<=(")|). That contains 2 alternatives, the first of length 1 (quote character) and the second of length 0 (an empty string), so the matched width could be either 0 or 1.