python - Unicode re.sub() 不适用于\g<0>(第 0 组)

标签 python regex string unicode regex-group

为什么 \g<0> 没有使用 unicode 正则表达式?

当我尝试使用 \g<0> 时使用普通字符串正则表达式在组前后插入一个空格,它有效:

>>> punct = """,.:;!@#$%^&*(){}{}|\/?><"'"""
>>> rx = re.compile('[%s]' % re.escape(punct))
>>> text = '''"anständig"'''
>>> rx.sub(r" \g<0> ",text)
' " anst\xc3\xa4ndig " '
>>> print rx.sub(r" \g<0> ",text)
 " anständig " 

但使用 unicode 正则表达式时,不会添加空格:

>>> punct = u""",–−—’‘‚”“‟„!£"%$'&)(+*-€/.±°´·¸;:=<?>@§#¡•[˚]»_^`≤…\«¿¨{}|"""
>>> rx = re.compile("["+"".join(punct)+"]", re.UNICODE)
>>> text = """„anständig“"""
>>> rx.sub(ur" \g<0> ", text)
'\xe2\x80\x9eanst\xc3\xa4ndig\xe2\x80\x9c'
>>> print rx.sub(ur" \g<0> ", text)
„anständig“
  1. 如何获得 \g在 unicode 正则表达式中工作?
  2. 如果 (1) 不可能,我如何让 unicode 正则表达式输入 punct 中字符前后的空格? ?

最佳答案

我认为你有两个错误。首先,您没有像第一个示例中那样使用 re.escape 转义 punct 并且您有像 [] 这样的字符需要转义。其次,text 变量不是 unicode。有效的例子:

>>> punct = re.escape(u""",–−—’‘‚”“‟„!£"%$'&)(+*-€/.±°´·¸;:=<?>@§#¡•[˚]»_^`≤…\«¿¨{}|""")
>>> rx = re.compile("["+"".join(punct)+"]", re.UNICODE)
>>> text = u"""„anständig“"""
>>> print rx.sub(ur" \g<0> ", text)
 „ anständig “

关于python - Unicode re.sub() 不适用于\g<0>(第 0 组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19427548/

相关文章:

c - 为什么strtof总是输出0.0000?

python - 如何在 Python 中迭代 GCP 项目中的所有 DataProc 集群?

python - 在Python中使用curses进行日志记录

regex - Perl/regex 日期格式转换

Java:打印 int[] 时出现奇怪的异常

javascript - 使用 javascript 和 MathJax 将数学公式字符串添加到文档

python - 在 Jupyter Notebook 中运行 FEniCS

python - pandas read_csv 中的转义引号

python - 删除引号之间的所有逗号

regex - 在正则表达式的特定点过滤输入量?