How to match patterns "starts with A or ends with B" with a Python regex?

r'(^|^A)(\S+)(B$|$)'

to match all that is actually ^ \ S $.

How to write one match "starts with A or ends with B, can both, but not both?"

PS: I also need to refer to the group (\ S +) in the subscript module.

Example:

Contrast Aanything, anythingBand open the anythinggroup to be replaced.

+3
source share
6 answers

The problem is resolved.

I use this regex in python, I found this in the Python manual:

(?(id/name)yes-pattern|no-pattern) yes-pattern, , . no-pattern . , (<)? (\ w + @\w + (?:.\w +) +) (? (1) > ) , '' as "user@host.com", '

2.4.

, :

r'(?P<prefix>A)?(?P<key>\S+)(?(prefix)|B)'

>>>re.sub(r'(?P<prefix>A)?(?P<key>\S+)(?(prefix)|B)','\g<key>',"Aanything")

'-'

>>>re.sub(r'(?P<prefix>A)?(?P<key>\S+)(?(prefix)|B)','\g<key>',"anythingB")

'-'

AanythingB anythingB , .

>>>re.sub(r'(?P<prefix>A)?(?P<key>\S+)(?(prefix)|B)','\g<key>',"AanythingB")

'anythingB'

+1
(^A.*B$)|(^A.*$)|(^.*B$)
+3

^A|B$ ^A|.*B$ ( )

UPDATE

.

:

match = re.match(r'^(?:A(\S+))|(?:(\S+)B)$', string)
if match:
    capture = max(match.groups())
# because match.groups() is either (capture, None) or (None, capture)
+2

?

var rx = /^((?:A)?)(.*?)((?:B)?)$/;
"Aanything".match(rx)
> ["Aanything", "A", "anything", ""]
"anythingB".match(rx)
> ["anythingB", "", "anything", "B"]
"AanythingB".match(rx)
> ["AanythingB", "A", "anything", "B"]
"anything".match(rx)
> ["anything", "", "anything", ""]
"AanythingB".replace(rx, '$1nothing$3');
> "AnothingB"
"AanythingB".replace(rx, '$2');
> "anything"
+2

:

/(^A|B$)/
+1

, "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.

0
source

Source: https://habr.com/ru/post/1770052/


All Articles