Skip to main content
 首页 » 编程设计

python之模式 : Python Regex 的意外结束

2025年05月04日15zhengyun_ustc

当我使用以下 python 正则表达式执行下面描述的功能时,我收到错误 Unexpected end of Pattern。

正则表达式:

modified=re.sub(r'^(?i)((?:(?!http://)(?!testing[0-9])(?!example[0-9]).)*?)(?-i) 
(CODE[0-9]{3})(?!</a>)',r'<a href="http://productcode/\g<1>">\g<1></a>',input) 

此正则表达式的用途:

输入:

CODE876 
CODE223 
matchjustCODE657 
CODE69743 
code876 
testing1CODE888 
example2CODE098 
http://replaced/CODE665 

应该匹配:

CODE876 
CODE223 
CODE657 
CODE697 

并将出现的地方替换为

http://productcode/CODE876 
http://productcode/CODE223 
matchjusthttp://productcode/CODE657 
http://productcode/CODE69743 

不应该匹配:

code876 
testing1CODE888 
testing2CODE776 
example3CODE654 
example2CODE098 
http://replaced/CODE665 

最终输出

http://productcode/CODE876 
http://productcode/CODE223 
matchjusthttp://productcode/CODE657 
http://productcode/CODE69743 
code876 
testing1CODE888 
example2CODE098 
http://replaced/CODE665 

编辑和更新 1

modified=re.sub(r'^(?i)((?:(?!http://)(?!testing[0-9])(?!example[0-9]).)*?)(CODE[0-9]{3})(?!</a>)',r'<a href="http://productcode/\g<1>">\g<1></a>',input) 

错误不再发生。但这并不符合任何需要的模式。是匹配组有问题还是匹配本身有问题。因为当我这样编译这个正则表达式时,我无法匹配我的输入。

编辑和更新 2

f=open("/Users/mymac/Desktop/regex.txt") 
s=f.read() 
 
s1 = re.sub(r'((?!http://|testing[0-9]|example[0-9]).*?)(CODE[0-9]{3})(?!</a>)',  
            r'\g<1><a href="http://productcode/\g<2>">\g<2></a>', s) 
print s1 

输入

CODE123 CODE765 testing1CODE123 example1CODE345 http://www.coding.com/CODE333 CODE345 
 
CODE234 
 
CODE333 

输出

<a href="http://productcode/CODE123">CODE123</a> <a href="http://productcode/CODE765">CODE765</a> testing1<a href="http://productcode/CODE123">CODE123</a> example1<a href="http://productcode/CODE345">CODE345</a> http://www.coding.com/<a href="http://productcode/CODE333">CODE333</a> <a href="http://productcode/CODE345">CODE345</a> 
 
<a href="http://productcode/CODE234">CODE234</a> 
 
<a href="http://productcode/CODE333">CODE333</a> 

正则表达式适用于原始输入,但不适用于来自文本文件的字符串输入。

有关更多结果,请参阅输入 4 和 5 http://ideone.com/3w1E3

请您参考如下方法:

你的主要问题是 (?-i) 事情,就 Python 2.7 和 3.2 而言,这是一厢情愿的想法。有关详细信息,请参见下文。

import re 
# modified=re.sub(r'^(?i)((?:(?!http://)(?!testing[0-9])(?!example[0-9]).)*?)(?-i) 
# (CODE[0-9]{3})(?!</a>)',r'<a href="http://productcode/\g<1>">\g<1></a>',input) 
# observation 1: as presented, pattern has a line break in the middle, just after (?-i) 
# ob 2: rather hard to read, should use re.VERBOSE 
# ob 3: not obvious whether it's a complile-time or run-time problem 
# ob 4: (?i) should be at the very start of the pattern (see docs) 
# ob 5: what on earth is (?-i) ... not in 2.7 docs, not in 3.2 docs 
pattern = r'^(?i)((?:(?!http://)(?!testing[0-9])(?!example[0-9]).)*?)(?-i)(CODE[0-9]{3})(?!</a>)' 
#### rx = re.compile(pattern) 
# above line failed with "sre_constants.error: unexpected end of pattern" 
# try without the (?-i) 
pattern2 = r'^(?i)((?:(?!http://)(?!testing[0-9])(?!example[0-9]).)*?)(CODE[0-9]{3})(?!</a>)' 
rx = re.compile(pattern2) 
# This works, now you need to work on observations 1 to 4, 
# and rethink your CODE/code strategy 

看起来建议被置若罔闻......这是 re.VERBOSE 格式的模式:

pattern4 = r''' 
    ^ 
    (?i) 
    ( 
        (?: 
            (?!http://) 
            (?!testing[0-9]) 
            (?!example[0-9]) 
            . #### what is this for? 
        )*? 
    ) ##### end of capturing group 1 
    (CODE[0-9]{3}) #### not in capturing group 1 
    (?!</a>) 
    '''