Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理'++';登录python正则表达式_Python_Regex - Fatal编程技术网

处理'++';登录python正则表达式

处理'++';登录python正则表达式,python,regex,Python,Regex,我有一个单词列表 我正在基于这个单词列表创建一个regex对象列表 import re word = 'This is word of spy++' wl = ['spy++','cry','fpp'] regobjs = [re.compile(r"\b%s\b" % word.lower() ) for word in wl] for reobj in regobjs: print re.search(regobj, word).group() 但是我在创建regex objs时

我有一个单词列表
我正在基于这个单词列表创建一个regex对象列表

import re
word = 'This is word of spy++'
wl = ['spy++','cry','fpp']
regobjs = [re.compile(r"\b%s\b" % word.lower() ) for word in wl]

for reobj in regobjs:
    print re.search(regobj, word).group()
但是我在创建regex objs时,由于符号的原因,出现了错误
(错误:多次重复)
++ 如何使正则表达式处理单词列表中所有单词的大小写

    requirements:

       regex should detect the exact word from the given text
 even if the word having non alpha numeric chars like (++) above code detect the exact words except those having ++ char.
除了
re.escape()
之外,您还需要删除非字母数字字符前后的
\b
单词边界,否则匹配将失败

类似这样的东西(不是很优雅,但我希望它能让人明白这一点):


当您的单词以字母、数字或下划线开头或结尾时,您希望使用
\b
;当单词不以字母、数字或下划线开头或结尾时,您希望使用
\b
。例如,这意味着您不会选择
spy++x
,而是选择
spy++.
甚至
spy++
。如果你想避免最后一个,那么事情会变得更加复杂

>>> def match_word(word):
    return re.compile("%s%s%s" % (
        "\\b" if word[0].isalnum() or word[0]=='_' else "\\B",
        re.escape(word.lower()),
        "\\b" if word[-1].isalnum() or word[-1]=='_' else "\\B"))

>>> text = 'This is word of spy++'
>>> wl = ['spy++','cry','fpp', 'word']
>>> for word in wl:
    match = re.search(match_word(word), text)
    if match:
        print(repr(match.group()))
    else:
        print("{} did not match".format(word))


'spy++'
cry did not match
fpp did not match
'word'
萨西

你的问题很糟糕,它不能表达你到底想要什么。然后人们会试图从代码的内容中扣除您想要的内容,这会导致混乱

我假设您希望在列表wl中查找单词的出现位置,而这些单词完全孤立在一个字符串中,也就是说,在每个出现位置周围没有任何非空格

如果是这样,我建议在以下代码中使用正则表达式的模式:

import re

ss = 'spy++ This !spy++ is spy++! word of spy++'
print ss
print [mat.start() for mat in re.finditer('spy',ss)]
print


base = ('(?:(?<=[ \f\n\r\t\v])|(?<=\A))'
        '%s'
        '(?=[ \f\n\r\t\v]|\Z)')

for x in ['spy++','cry','fpp']:
    print x,[mat.start() for mat in re.finditer(base % re.escape(x),ss)]

你需要。@SvenMarnach:他需要的不止这些…@Sashi没有人想得到错误。想要“不获得”并不会带来想要“获得”的信息。编写“处理所有情况”非常模糊,但如果我想匹配给定字符串中的确切单词,它是否有效?这就是我添加\b的原因。它不是在存在\b需要选项\b以及re.escape()的情况下进行精确匹配,或者它们是否有其他解决方案?抱歉,删除了我的上一条评论,它不适用于Python。Python在其lookbehind中甚至不接受替换。@Tim,您没有测试这段代码吗?必须在
word='这是spy++'和
print re.search(regobj,word).group()
中的名称词中添加
s
,以使此代码正确工作,否则会与循环中的词冲突。-顺便说一句,regobj而不是reobj…@Tim如果我们假设Sashi想要找到子字符串,其第一个和最后一个字符是属于符号
\b
文档中定义为单词的字符(也就是说:“单词被定义为字母数字或下划线字符的序列”),那么如果eword[0].isalnum()
如果eword[-1].isalnum()
必须添加
或eword[0]='''
或eword[-1]='=''
-但Sashi实际上似乎希望找到由空格或空格分隔的单词。您的代码在“word of!spy++',在“spy++!”中,在"间谍的话"中,。我不确定这是萨西想要的。事实上,他的要求令人困惑。@eyquem是的,要求令人困惑。如果他为单词和非单词之间的界限指定了确切的规则,那么就有可能匹配这些规则。
import re

ss = 'spy++ This !spy++ is spy++! word of spy++'
print ss
print [mat.start() for mat in re.finditer('spy',ss)]
print


base = ('(?:(?<=[ \f\n\r\t\v])|(?<=\A))'
        '%s'
        '(?=[ \f\n\r\t\v]|\Z)')

for x in ['spy++','cry','fpp']:
    print x,[mat.start() for mat in re.finditer(base % re.escape(x),ss)]
spy++ This !spy++ is spy++! word of spy++
[0, 12, 21, 36]

spy++ [0, 36]
cry []
fpp []