【问题标题】:how to search keyword without ordering and search keywords which embedded special character without ordering如何无序搜索关键字和无序搜索嵌入特殊字符的关键字
【发布时间】: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) 永远无法匹配,因为一个字符不能同时是ke
  • 我编辑为 '(?=k|e|y|[ \$\@]*|\ |w|o|r|d|s)'

标签: python regex


【解决方案1】:

你想要这样的东西吗?

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

[附注]

你想做的可能是这样的

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

【讨论】:

  • easysearch(row1, r'key wrods') 怎么样, o 和 r 交换了,看每个字符没有排序,如何用这个来指定每个特殊字符 '(?=$*)( ?=@*)(?= )' 自定义期望的特殊字符并且可以在没有排序的情况下重复
  • 对不起,我错过了按你的答案编辑按钮,但我取消了编辑,它返回到以前的答案,我更新了问题,案例 3 和案例 4 需要帮助
  • 如果你想指定特殊字符,我建议你使用这样的字符类,[\$\@ ]+。因此替换脚本将更改为re.sub(r' ', r'[\$\@ ]+', 'key words', flags=re.IGNORECASE)
  • case 3 和 case 4 无法通过 re.findall(re.sub(r' ', r'[\$\@ ]+', 'key words', flags=re. IGNORECASE), row1, re.DOTALL),如何同时考虑 case 3 和 case 4?
  • 我现在再次更新了我的答案。请检查并重试。谢谢:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-31
  • 2011-10-27
  • 1970-01-01
  • 2019-12-13
  • 2012-02-22
  • 2010-10-07
相关资源
最近更新 更多