Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则十六进制_Python_Regex - Fatal编程技术网

Python 正则十六进制

Python 正则十六进制,python,regex,Python,Regex,以下字符串 str1 = "Hello\x00MORE\x11\x20TEXT\x05here" 应转换为此列表(删除所有十六进制字符串,\x20除外,该字符串应为空白): 到目前为止,我的解决方案是: new_list = re.split('\s+', re.sub('[\x01-\x1f\x7f]', ' ', s)) 它产生: list1 = ['Hello', 'MORE', 'TEXT', 'here'] 但正如您所看到的,我需要更多的文本成为一个字符串,因此\x20应该转换为

以下字符串

str1 = "Hello\x00MORE\x11\x20TEXT\x05here"
应转换为此列表(删除所有十六进制字符串,\x20除外,该字符串应为空白):

到目前为止,我的解决方案是:

new_list = re.split('\s+', re.sub('[\x01-\x1f\x7f]', ' ', s))
它产生:

list1 = ['Hello', 'MORE', 'TEXT', 'here']
但正如您所看到的,我需要更多的文本成为一个字符串,因此
\x20
应该转换为空白。除了不考虑<代码> \x20

之外,如何使用正则表达式?
>>> [re.sub(r'[\x00-\x1f\x7f ]+',' ',i) for i in re.split(r'(?<=[a-z])[\x00-\x1f\x7f]|[\x00-\x1f\x7f](?=[a-z])',str1)]
['Hello', 'MORE TEXT', 'here']

现在,您需要用空字符串替换十六进制数,或者像我所做的那样,您可以用空格替换十六进制数的任意组合。

FYI:在本例中\x20没有例外您从不同的帐户询问此问题的任何原因?不是我。然而,我看到了这个问题,也遇到了同样的问题,但有一个小小的\x20例外
>>> [re.sub(r'[\x00-\x1f\x7f ]+',' ',i) for i in re.split(r'(?<=[a-z])[\x00-\x1f\x7f]|[\x00-\x1f\x7f](?=[a-z])',str1)]
['Hello', 'MORE TEXT', 'here']
r'(?<=[a-z])[\x00-\x1f\x7f]|[\x00-\x1f\x7f](?=[a-z])'
['Hello', 'MORE\x11 TEXT', 'here']