如何无序搜索关键字和无序搜索嵌入特殊字符的关键字

2024-09-28 05:19:07 发布

您现在位置:Python中文网/ 问答频道 /正文

更新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')

Tags: keyresearchtodaywordsrow1casese
1条回答
网友
1楼 · 发布于 2024-09-28 05:19:07

你想要这样的东西吗?你知道吗

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']

我只是在re.sub()函数中更改了您的replacement strings,并将输入错误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']

相关问题 更多 >

    热门问题