, "A" "B", :
reMatcher= re.compile(r"(?<=\AA).*|.*(?=B\Z)")
( \A ^ \Z $)
"" ( "" ), "" "" :
'A text here' matches ' text here'
'more text hereB' matches 'more text here'
'AYES!B' matched 'AYES!'
'neither' doesn't match
( , "Pythonic" ):
def strip_prefix_suffix(text, prefix, suffix):
left = len(prefix) if text.startswith(prefix) else 0
right= -len(suffix) if text.endswith(suffix) else None
return text[left:right] if left or right else None
, None, '' (, strip_prefix_suffix('AB', 'A', 'B')).
PS , :
(?<=\AA).*(?=B\Z)|(?<=\AA).*|.*(?=B\Z)
should work, but it is not; it works the same as the one I suggested, and I cannot understand why. Breaking a regular expression into parts, we can see something strange:
>>> text= 'AYES!B'
>>> re.compile('(?<=\\AA).*(?=B\\Z)').search(text).group(0)
'YES!'
>>> re.compile('(?<=\\AA).*').search(text).group(0)
'YES!B'
>>> re.compile('.*(?=B\\Z)').search(text).group(0)
'AYES!'
>>> re.compile('(?<=\\AA).*(?=B\\Z)|(?<=\\AA).*').search(text).group(0)
'YES!'
>>> re.compile('(?<=\\AA).*(?=B\\Z)|.*(?=B\\Z)').search(text).group(0)
'AYES!'
>>> re.compile('(?<=\\AA).*|.*(?=B\\Z)').search(text).group(0)
'AYES!'
>>> re.compile('(?<=\\AA).*(?=B\\Z)|(?<=\\AA).*|.*(?=B\\Z)').search(text).group(0)
'AYES!'
For some strange reason, subexpression .*(?=B\\Z)takes precedence, although this is the last alternative.
source
share