pythonpython-reregex-negationpython-regex

How to neglect a backslash of the text while using regular expression re.search in python?


import re
a = "apple\c"
b = "applec"

re.search(pattern, a)
re.search(pattern, b)

while searching the pattern. In example ".+" for Any char one or more reptitions. Here I want to neglect "\" when ".+" identify "\" character in search.


Solution

  • By "neglect" do you mean remove from the output or don't include in the pattern, i.e. find "apple"?

    If former:

    >>> a.replace('\\', '')
    'applec'
    

    if latter:

    >>> re.search(r'[^\\]*', a)
    <re.Match object; span=(0, 5), match='apple'>