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

37 Views Asked by At
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.

1

There are 1 best solutions below

0
Yevhen Kuzmovych On BEST ANSWER

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'>