【发布时间】:2018-04-11 03:46:59
【问题描述】:
更新 2
我编辑你的建议做 5 个案例,几乎可以做所有案例, 但它有额外的结果,多余的结果,如何改进或有其他更好的解决方案?
case 1
row1 = "search key $ @ $ words today"
re.findall(r'[(?=k&e&y&w&o&r&d&s)]*[ \$\@]*', row1, re.DOTALL)
['se', '', 'r', '', '', ' ', 'key $ @ $ ', 'words ', '', 'od', '', 'y', '']
case 2
row1 = "search key words today"
re.findall(r'[(?=k&e&y&w&o&r&d&s)]*[ \$\@]*', row1, re.DOTALL)
['se', '', 'r', '', '', ' ', 'key ', 'words ', '', 'od', '', 'y', '']
case 3 need help
row1 = "search key wrods today"
re.findall(r'[(?=k&e&y&w&o&r&d&s)]*[ \$\@]*', row1, re.DOTALL)
['se', '', 'r', '', '', ' ', 'key ', 'wrods ', '', 'od', '', 'y', '']
case 4 need help
row1 = "search key $ @ $ wrods today"
re.findall(r'[(?=k&e&y&w&o&r&d&s)]*[ \$\@]*', row1, re.DOTALL)
['se', '', 'r', '', '', ' ', 'key $ @ $ ', 'wrods ', '', 'od', '', 'y', '']
case 5
row1 = "search key wrds today"
re.findall(r'[(?=k&e&y&w&o&r&d&s)]*[ \$\@]*', row1, re.DOTALL)
['se', '', 'r', '', '', ' ', 'key ', 'wrds ', '', 'od', '', 'y', '']
更新 1
case 1 is ok
row1 = "search key $ @ $ words today"
re.sub(r' ',r'[ \$\@]*', r'key words')
re.findall(re.sub(r' ',r'[ \$\@]*', r'key words'), row1, re.DOTALL)
case 2 is ok
row1 = "search key words today"
re.sub(r' ',r'[ \$\@]*', r'key words')
re.findall(re.sub(r' ',r'[ \$\@]*', r'key words'), row1, re.DOTALL)
case 3 need help
row1 = "search key wrods today"
re.sub(r' ',r'[ \$\@]*', r'key words')
re.findall(r'(?=k|e|y|[ \$\@]*|\ |w|o|r|d|s)', row1, re.DOTALL)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
case 4 need help
row1 = "search key $ @ $ wrods today"
re.sub(r' ',r'[ \$\@]*', r'key words')
re.findall(r'(?=k|e|y|[ \$\@]*|\ |w|o|r|d|s)', row1, re.DOTALL)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
A.
因为
row1 = "search key words today"
searchresult = re.findall(re.sub(r' ', ' *', r'key wrods', flags=re.IGNORECASE), row1, re.DOTALL)
r 和 o 互换,
然后我使用 (?=
searchresult = re.findall(re.sub(r' ', ' *', r'(?=k)(?=e)(?=y)(?=\ )(?=w)(?=r)(?=o)(?=d)(?=s)', flags=re.IGNORECASE), row1, re.DOTALL)
searchresult
[]
返回空值
如果重复字符,关键字是“kkey wods”怎么办?
B.
从嵌入任意顺序的特殊字符的内容中搜索关键字时包含任意顺序的特殊字符时出现错误
row1 = "search key $ @ $ words today"
re.sub(r' ',r'(?=$*)(?= *)*', r'key words')
re.findall(re.sub(r' ',r'(?=$*)(?=@*)(?= *)*', r'key words'), row1, re.DOTALL)
>>> re.findall(re.sub(r' ',r'(?=$*)(?=@*)(?= *)*', r'key words'), row1, re.DOTALL)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\re.py", line 181, in findall
return _compile(pattern, flags).findall(string)
File "C:\Python27\lib\re.py", line 251, in _compile
raise error, v # invalid expression
sre_constants.error: 无需重复
只为
row1 = "search key words today"
def easysearch(content, keywords):
row1 = content
searchresult = re.findall(re.sub(r' ', ' *', keywords, flags=re.IGNORECASE), row1, re.DOTALL)
if count_chars(searchresult[0])/count_chars(keywords) > 0.5:
return searchresult
easysearch(row1, r'key words')
easysearch(row1, r'key wrods')
row1 = "search key $ @ $ words today"
def easysearch(content, keywords):
row1 = content
searchresult = re.findall(re.sub(r' ',r'(?=$*)(?=@*)(?= *)*', keywords), row1, re.DOTALL)
if count_chars(searchresult[0])/count_chars(keywords) > 0.5:
return searchresult
easysearch(row1, r'key words')
easysearch(row1, r'key wrods')
【问题讨论】:
-
我不知道你要做什么,但
(?=k)(?=e)永远无法匹配,因为一个字符不能同时是k和e。 -
我编辑为 '(?=k|e|y|[ \$\@]*|\ |w|o|r|d|s)'