如何在一长串字符中识别正则表达式中的坏字符?

时间:2017-08-14 09:02:50

标签: python regex perl

目标是将此Perl正则表达式(从here)移植到Python:

$norm_text =~ s/(\P{N})(\p{P})/$1 $2 /g;

首先,我已将\p{P}\P{N}字符数组复制到可读文本文件中:

import requests
from six import text_type

n_url = 'https://raw.githubusercontent.com/alvations/charguana/master/charguana/data/perluniprops/Number.txt'
p_url = 'https://raw.githubusercontent.com/alvations/charguana/master/charguana/data/perluniprops/Punctuation.txt'

NUMS = text_type(requests.get(n_url).content.decode('utf8'))
PUNCTS = text_type(requests.get(p_url).content.decode('utf8'))

但是当我尝试编译正则表达式时:

re.compile(u'([{n}])([{p}])'.format(n=NUMS, p=PUNCTS)

它抛出了这个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/alvas/anaconda3/lib/python3.6/re.py", line 233, in compile
    return _compile(pattern, flags)
  File "/Users/alvas/anaconda3/lib/python3.6/re.py", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/Users/alvas/anaconda3/lib/python3.6/sre_compile.py", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "/Users/alvas/anaconda3/lib/python3.6/sre_parse.py", line 856, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
  File "/Users/alvas/anaconda3/lib/python3.6/sre_parse.py", line 415, in _parse_sub
    itemsappend(_parse(source, state, verbose))
  File "/Users/alvas/anaconda3/lib/python3.6/sre_parse.py", line 763, in _parse
    p = _parse_sub(source, state, sub_verbose)
  File "/Users/alvas/anaconda3/lib/python3.6/sre_parse.py", line 415, in _parse_sub
    itemsappend(_parse(source, state, verbose))
  File "/Users/alvas/anaconda3/lib/python3.6/sre_parse.py", line 552, in _parse
    raise source.error(msg, len(this) + 1 + len(that))
sre_constants.error: bad character range ~-- at position 217 (line 1, column 218)

环顾问题似乎是字符集中没有转义的短划线Python regex bad character range.

看起来像是一系列破折号符号:

>>> NUMS[215:352]
'~----------------------------------------------------------------------------------------------------------------------------------------'

然后我将破折号字符移到了字符串的前面,但是还有更糟糕的字符:

>>> NUMS2 = re.escape(NUMS[215:352]) + NUMS[:215] + NUMS[352:]
>>> re.compile(u'([{n}])'.format(n=NUMS2))

[OUT]:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/alvas/anaconda3/lib/python3.6/re.py", line 233, in compile
    return _compile(pattern, flags)
  File "/Users/alvas/anaconda3/lib/python3.6/re.py", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/Users/alvas/anaconda3/lib/python3.6/sre_compile.py", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "/Users/alvas/anaconda3/lib/python3.6/sre_parse.py", line 856, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
  File "/Users/alvas/anaconda3/lib/python3.6/sre_parse.py", line 415, in _parse_sub
    itemsappend(_parse(source, state, verbose))
  File "/Users/alvas/anaconda3/lib/python3.6/sre_parse.py", line 763, in _parse
    p = _parse_sub(source, state, sub_verbose)
  File "/Users/alvas/anaconda3/lib/python3.6/sre_parse.py", line 415, in _parse_sub
    itemsappend(_parse(source, state, verbose))
  File "/Users/alvas/anaconda3/lib/python3.6/sre_parse.py", line 552, in _parse
    raise source.error(msg, len(this) + 1 + len(that))
sre_constants.error: bad character range ¬-- at position 502 (line 1, column 503)

所以我把更多的角色移到了前面:

>>> NUMS2 = re.escape(NUMS[215:352]) + NUMS[:215] + NUMS[352:]
>>> NUMS3 = re.escape(NUMS2[500:504]) + NUMS2[:500] + NUMS2[504:]
>>> re.compile(u'([{n}])'.format(n=NUMS3))

这似乎是一个无休止的循环,检测什么是一个&#34;糟糕的角色范围&#34;在正则表达式。

有没有办法自动识别所有&#34;坏字符&#34;在正则表达式中并将它们转移到前面?

1 个答案:

答案 0 :(得分:3)

这里的要点是你需要在字符类中转义^-]\字符。

使用

NUMS = re.sub(r'[]^\\-]', r'\\\g<0>', NUMS)
PUNCTS = re.sub(r'[]^\\-]', r'\\\g<0>', PUNCTS)
rx = re.compile(u'([{n}])([{p}])'.format(n=NUMS, p=PUNCTS)

r'[]^\\-]'模式将匹配1个字符 - ]^\- - 并且r'\\\g<0>'替换将替换匹配值\和匹配值。