英文:
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 repetitions. Here I want to neglect "" when ".+" identifies "" character in search.
英文:
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
得分: 0
By "neglect" do you mean remove from the output or don't include in the pattern, i.e. find "apple"?
如果是前者:
>>> a.replace('\\', '')
'applec'
如果是后者:
>>> re.search(r'[^\\]*', a)
<re.Match object; span=(0, 5), match='apple'>
英文:
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'>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论