3
爲什麼\g<0>
不能用於unicode正則表達式?Unicode re.sub()不適用於 g <0>(組0)
當我試圖用\g<0>
之前與普通的字符串正則表達式組之後插入一個空間,它的工作原理:
>>> punct = """,.:;[email protected]#$%^&*(){}{}|\/?><"'"""
>>> 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「
- 我如何獲得
\g
採用Unicode正則表達式的工作? - 如果(1)不可能,我怎樣才能得到unicode regex輸入
punct
中的一個字符前後的空格?