开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜