开发者

issue while matching special char '\' in regular expression using '\\'

开发者 https://www.devze.com 2023-01-04 22:52 出处:网络
I have very simple python script to match some special characters like -,+,-,. But Im not getting expected result while using regex \\ to match a single \'\\\' char.

I have very simple python script to match some special characters like -,+,-,. But Im not getting expected result while using regex \ to match a single '\' char.

import re
pat = r'[-+*\\]'
text = 'fdkjdfk\sdsdd'
if re.search(pat,text):
   print re.search(pat,text).group()
else:
   print "not found"

On running above code , it prints 'not found' It seems I am doing some mistake 开发者_JAVA百科here , any help appreciated !!!


\ is an escape character.

Try escaping it:

text = 'fdkjdfk\\sdsdd' 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号