【问题标题】:Replacing unicode brackets in python替换python中的unicode括号
【发布时间】:2016-01-27 16:23:13
【问题描述】:

如何用空格填充 unicode 括号?

当我尝试使用re.sub 时,我得到一个sre_constants.error

>>> import re
>>> open_punct = ur'([{༺༼᚛‚„⁅⁽₍〈❨❪❬❮❰❲❴⟅⟦⟨⟪⟬⟮⦃⦅⦇⦉⦋⦍⦏⦑⦓⦕⦗⧘⧚⧼⸢⸤⸦⸨〈《「『【〔〖〘〚〝﴾︗︵︷︹︻︽︿﹁﹃﹇﹙﹛﹝([{⦅「'
>>> text = u'this is a weird ❴sentence ⟅with some crazy ⟦punctuations sprinkled⟨'
>>> re.sub(open_punct, ur'\1 ', text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/re.py", line 155, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/usr/lib/python2.7/re.py", line 251, in _compile
    raise error, v # invalid expression
sre_constants.error: unexpected end of regular expression

为什么会这样?为什么正则表达式意外结束?

当我尝试使用re.escape 时,它不会引发错误但re.sub 没有用空格填充标点符号:

>>> re.sub(re.escape(open_punct), ur'\1 ', text)
u'this is a weird \u2774sentence \u27c5with some crazy \u27e6punctuations sprinkled\u27e8'
>>> print re.sub(re.escape(open_punct), ur'\1 ', text)
this is a weird ❴sentence ⟅with some crazy ⟦punctuations sprinkled⟨

我可以这样做,但我希望正则表达式解决方案应该比循环更优化:

>>> for p in open_punct:
...     text = text.replace(p, p+' ')
... 
>>> text
u'this is a weird \u2774 sentence \u27c5 with some crazy \u27e6 punctuations sprinkled\u27e8 '
>>> print text
this is a weird ❴ sentence ⟅ with some crazy ⟦ punctuations sprinkled⟨ 
>>> open_punct
u'([{\u0f3a\u0f3c\u169b\u201a\u201e\u2045\u207d\u208d\u2329\u2768\u276a\u276c\u276e\u2770\u2772\u2774\u27c5\u27e6\u27e8\u27ea\u27ec\u27ee\u2983\u2985\u2987\u2989\u298b\u298d\u298f\u2991\u2993\u2995\u2997\u29d8\u29da\u29fc\u2e22\u2e24\u2e26\u2e28\u3008\u300a\u300c\u300e\u3010\u3014\u3016\u3018\u301a\u301d\ufd3e\ufe17\ufe35\ufe37\ufe39\ufe3b\ufe3d\ufe3f\ufe41\ufe43\ufe47\ufe59\ufe5b\ufe5d\uff08\uff3b\uff5b\uff5f\uff62'
>>> print open_punct
([{༺༼᚛‚„⁅⁽₍〈❨❪❬❮❰❲❴⟅⟦⟨⟪⟬⟮⦃⦅⦇⦉⦋⦍⦏⦑⦓⦕⦗⧘⧚⧼⸢⸤⸦⸨〈《「『【〔〖〘〚〝﴾︗︵︷︹︻︽︿﹁﹃﹇﹙﹛﹝([{⦅「

相关问题:

【问题讨论】:

    标签: python regex unicode punctuation


    【解决方案1】:

    [( 在正则表达式中具有特殊含义,解析器正在寻找它们的 ]) 对应项。

    如果您的意思是让open_punct 成为一个字符组,无论如何您都应该用[..] 括起来所有字符,此时([ 都可以包括未转义。您的“表达式”仅匹配具有所有这些字符按该顺序显示的文本

    由于您还希望引用捕获组 (\1),因此添加括号:

    >>> re.sub(u'([{}])'.format(open_punct), ur'\1 ', text)
    u'this is a weird \u2774 sentence \u27c5 with some crazy \u27e6 punctuations sprinkled\u27e8 '
    >>> print re.sub(u'([{}])'.format(open_punct), ur'\1 ', text)
    this is a weird ❴ sentence ⟅ with some crazy ⟦ punctuations sprinkled⟨
    

    请注意,使用re.escape() 仍然是一个好主意,以防您要匹配的组中有-] 字符,或\[group] 序列。 - 定义了一个字符序列(0-9 为所有数字),] 组的结尾,\d\w\s 等,都定义了预定义的字符组:

    re.sub(u'([{}])'.format(re.escape(open_punct)), ur'\1 ', text)
    

    【讨论】:

    • 为什么([{}])中需要大括号?
    • @alvas:它们是str.format() 占位符。 open_punct 的值放置在该位置的字符串中,形成字符串u'([[([{༺༼᚛‚„⁅⁽₍〈❨❪❬❮❰❲❴⟅⟦⟨⟪⟬⟮⦃⦅⦇⦉⦋⦍⦏⦑⦓⦕⦗⧘⧚⧼⸢⸤⸦⸨〈《「『【〔〖〘〚〝﴾︗︵︷︹︻︽︿﹁﹃﹇﹙﹛﹝([{⦅「])'
    • 哦,我瞎了...我以为我看到了正则表达式。哈哈哈.. 谢谢 Martijn!
    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    相关资源
    最近更新 更多