更新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', '']
udpate 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)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
答
自
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
[]
返回null
如果重复字符,关键字是“kkey wrods”,情况如何?你知道吗
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公司_常量.错误:无需重复
只是为了
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')
你想要这样的东西吗?你知道吗
输出为
我只是在
re.sub()
函数中更改了您的replacement strings
,并将输入错误wrods
修复为words
对于案例3、4(更新版)
你可以试试这个
输出为
[p.S]
你想做的可能是这样的
输出为
相关问题 更多 >
编程相关推荐