更新2
我编辑你的建议做5例,附近可以做所有的情况, 但它有额外的结果,多余的结果,如何改进或是否有另一个更好的解决方案?
output=${input%risk/}risk_input/
udpate 1
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', '']
一个。
因为
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)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
r和o被交换,
然后我搜索没有订单(?=
row1 = "search key words today"
searchresult = re.findall(re.sub(r' ', ' *', r'key wrods', flags=re.IGNORECASE), row1, re.DOTALL)
返回null
如果重复的字符,关键字是" kkey wrods"
的情况如何?乙
当从任何顺序嵌入特殊字符的内容中搜索关键字包含任何顺序的特殊字符
时,会出现错误searchresult = re.findall(re.sub(r' ', ' *', r'(?=k)(?=e)(?=y)(?=\ )(?=w)(?=r)(?=o)(?=d)(?=s)', flags=re.IGNORECASE), row1, re.DOTALL)
searchresult
[]
sre_constants.error:无需重复
仅适用于
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
答案 0 :(得分:0)
你想要这样的东西吗?
row1 = "search key words today"
searchresult = re.findall(re.sub(r' ', ' *', r'key words', flags=re.IGNORECASE), row1, re.DOTALL)
print(searchresult)
row1 = "search key $ @ $ words today"
searchresult = re.findall(re.sub(r' ',r' .*?', r'key words'), row1, re.DOTALL)
print(searchresult)
输出
['key words']
['key $ @ $ words']
我刚刚更改了replacement strings
功能中的re.sub()
并修正了错误wrods
至words
案例3,4(已更新)
你可以尝试这个脚本
row1 = "search eky sky key wrods today"
print(re.findall(r'(?:(?=k|e|y|[ \$\@]+|w|o|r|d|s).)+', row1, re.DOTALL))
row1 = "search yek key $ @ sky $ wrods today"
print(re.findall(r'(?:(?=k|e|y|[ \$\@]+|w|o|r|d|s).)+', row1, re.DOTALL))
输出
['se', 'r', ' eky sky key wrods ', 'od', 'y']
['se', 'r', ' yek key $ @ sky $ wrods ', 'od', 'y']
<强> [P.S] 强>
你想做的事情可能是这样的
row1 = "search eky wrods today"
print(re.findall(r'(?:k|e|y)+[ \$\@]+(?:w|o|r|d|s)+', row1, re.DOTALL))
row1 = "search yek $ @ $ wrods today"
print(re.findall(r'(?:k|e|y)+[ \$\@]+(?:w|o|r|d|s)+', row1, re.DOTALL))
输出
['eky wrods']
['yek $ @ $ wrods']