Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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_Parsing_Markup - Fatal编程技术网

Python 要替换的正则表达式";逃过;带原件的字符

Python 要替换的正则表达式";逃过;带原件的字符,python,regex,parsing,markup,Python,Regex,Parsing,Markup,注意:我没有用正则表达式解析大量的html或通用html。我知道这很糟糕 TL;博士: 我有像 A sentence with an exclamation\! Next is a \* character 原始标记中有“转义”字符的位置。我希望用它们的“原件”来代替它们。并获得: A sentence with an exclamation! Next is a * character 我需要从一些wiki标记中提取一小部分数据 我这里只处理段落/片段,所以我不需要一个强大的解决方案。在

注意:我没有用正则表达式解析大量的html或通用html。我知道这很糟糕

TL;博士

我有像

A sentence with an exclamation\! Next is a \* character
原始标记中有“转义”字符的位置。我希望用它们的“原件”来代替它们。并获得:

A sentence with an exclamation! Next is a * character

我需要从一些wiki标记中提取一小部分数据

我这里只处理段落/片段,所以我不需要一个强大的解决方案。在python中,我尝试了一个测试:

s = "test \\* \\! test * !! **"

r = re.compile("""\\.""") # Slash followed by anything

r.sub("-", s)
这应该是:

test - - test * !! **
但它什么也没用。我是不是遗漏了什么

此外,我不知道如何将任何给定的转义字符替换为其原始字符,因此我可能只需要创建一个列表,并使用特定的正则表达式进行sub,如:

\\\*


可能有一种更简洁的方法可以做到这一点,因此非常感谢您的帮助。

您缺少了一些东西,即
r
前缀:

r = re.compile(r"\\.") # Slash followed by anything
python和
re
都赋予
\
以意义;当您将字符串值传递给
re.compile()
时,您的双反斜杠变为一个反斜杠,此时
re
会看到
\.
,表示文字上的句号:

>>> print """\\."""
\.
通过使用
r'
您告诉python不要解释转义码,因此现在
re
被赋予一个带有
\.
的字符串,意思是后跟任意字符的反斜杠:

>>> print r"""\\."""
\\.
演示:

经验法则是:在定义正则表达式时,使用
r'
raw string文本,这样您就不必双重转义对Python和正则表达式语法都有意义的所有内容

接下来,要替换“转义”字符;为此使用组,
re.sub()
允许您引用组作为替换值:

r = re.compile(r"\\(.)") # Note the parethesis, that's a capturing group
r.sub(r'\1', s)          # \1 means: replace with value of first capturing group
现在输出为:

>>> r = re.compile(r"\\(.)") # Note the parethesis, that's a capturing group
>>> r.sub(r'\1', s) 
'test * ! test * !! **'

您缺少了一些内容,即
r
前缀:

r = re.compile(r"\\.") # Slash followed by anything
python和
re
都赋予
\
以意义;当您将字符串值传递给
re.compile()
时,您的双反斜杠变为一个反斜杠,此时
re
会看到
\.
,表示文字上的句号:

>>> print """\\."""
\.
通过使用
r'
您告诉python不要解释转义码,因此现在
re
被赋予一个带有
\.
的字符串,意思是后跟任意字符的反斜杠:

>>> print r"""\\."""
\\.
演示:

经验法则是:在定义正则表达式时,使用
r'
raw string文本,这样您就不必双重转义对Python和正则表达式语法都有意义的所有内容

接下来,要替换“转义”字符;为此使用组,
re.sub()
允许您引用组作为替换值:

r = re.compile(r"\\(.)") # Note the parethesis, that's a capturing group
r.sub(r'\1', s)          # \1 means: replace with value of first capturing group
现在输出为:

>>> r = re.compile(r"\\(.)") # Note the parethesis, that's a capturing group
>>> r.sub(r'\1', s) 
'test * ! test * !! **'

感谢您的明确和简洁的答复。我的印象是,python中的三重引用意味着从字面上理解字符串(如……尽可能从字面上理解)。不知道为什么,但现在我知道r前缀就是这么做的。谢谢三重引号可以轻松地包含换行符和单引号,而无需转义;您也可以将原始报价和三重报价结合起来。:-)感谢您的明确和简洁的答复。我的印象是,python中的三重引用意味着从字面上理解字符串(如……尽可能从字面上理解)。不知道为什么,但现在我知道r前缀就是这么做的。谢谢三重引号可以轻松地包含换行符和单引号,而无需转义;您也可以将原始报价和三重报价结合起来。:-)