简体   繁体   English

正则表达式,在python中使用搜索

[英]Regular Expressions, using search in python

I want to decode a some hex in python. 我想在python中解码一些十六进制。

In part of the string \\xcd\\xed\\xb0\\xb2 在字符串的一部分\\xcd\\xed\\xb0\\xb2

    text = re.search(r'(\\x\w{2}){4}', rtf)

    unicodeText = text.decode('gb2312')

Error: '_sre.SRE_Match' object has no attribute 'decode' 错误: “ _ sre.SRE_Match”对象没有属性“ decode”

Hope someone can help, Thanks 希望有人可以帮助,谢谢

re.search returns a Match object , not a matched string. re.search返回Match对象 ,而不是匹配的字符串。

Use group method to get the matched string. 使用group方法获取匹配的字符串。

>>> rtf = r'\xcd\xed\xb0\xb2'
>>> matched = re.search(r'(\\x\w{2}){4}', rtf)
>>> text = matched.group()
>>> text.decode('string-escape').decode('gb2312')
u'\u665a\u5b89'

# In Python 3.x
# >>> text.encode().decode('unicode-escape').encode('latin1').decode('gb2312')
# '晚安'

BTW, you don't need to use regular expression, what you want is convert \\xOO : 顺便说一句,您不需要使用正则表达式,您想要的是转换\\xOO

Python 2.x: Python 2.x:

>>> rtf = r'\xcd\xed\xb0\xb2'
>>> rtf.decode('string-escape').decode('gb2312')
u'\u665a\u5b89'
>>> print rtf.decode('string-escape').decode('gb2312')
晚安

Python 3.x: Python 3.x:

>>> rtf = r'\xcd\xed\xb0\xb2'
>>> rtf.encode().decode('unicode-escape').encode('latin1').decode('gb2312')
'晚安'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM