A regular expression matching a string followed by another string without capturing the last

Is it possible to make a view request not exciting? Things like bar(?:!foo) and bar(?!:foo) don't work (Python).

+5
source share
2 answers

If you do bar(?=ber) on "barber", "bar" is mapped, but "ber" is not fixed.

+3
source

You did not answer Alan's question, but I will assume that he is right, and you are interested in a negative statement. IOW - corresponds to "bar", but not to "barfoo". In this case, you can create your regular expression as follows:

 myregex = re.compile('bar(?!foo)') for example, from the python console: >>> import re >>> myregex = re.compile('bar(?!foo)') >>> m = myregex.search('barfoo') >>> print m.group(0) <=== Error here because match failed Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'group' >>> m = myregex.search('bar') >>> print m.group(0) <==== SUCCESS! bar 
+1
source

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


All Articles