gpt4 book ai didi

python - 如何使用正则表达式在具有特定规则的字符串中查找子字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 01:50:45 26 4
gpt4 key购买 nike

我有一个像下面这样的字符串

Hello there how are you?

我想在字符串中查找子字符串 'there how'。所以我会做这样的事情

import re
string = "Hello there how are you?"
term = "there how"
print(re.search("\s" + term + "\s", string).group(0)). # /s is used to ensure the match should be an independent phrase

但现在的问题是,如果我得到字符串的变体,则匹配不会发生。例如对于像这样的字符串

如果单词之间有大量空格

Hello there         how are you?

如果某些字母大写

Hello There How are you?

我想做的是确保只要子字符串 'there how' 作为单独的短语出现在字符串中(不像 Hellothere how are you?Hello there hoare you? 等),我应该能够找到匹配项。

我怎样才能实现目标?

最佳答案

您可以在 term 中用 \s+ 替换空格,并通过传递 re.I 标志来使用不区分大小写的匹配:

import re
ss = ["Hello there how are you?", "Hello there how are you?", "Hello There How are you?"]
term = "there how"
rx = re.compile(r"(?<!\S){}(?!\S)".format(term.replace(r" ", r"\s+")), re.I)

for s in ss:
m = re.search(rx, s)
if m:
print(m.group())

输出:

there how
there how
There How

参见 Python demo

注意:如果term可以包含特殊的正则表达式元字符,您需要re.escape term , 但在用 \s+ 替换空格之前执行此操作。由于使用 re.escape 转义了空格,因此您需要 .replace(r'\', r'\s+'):

rx = re.compile(r"(?<!\S){}(?!\S)".format(re.escape(term).replace(r"\ ", r"\s+")), re.I)

JavaScript 解决方案:

var ss = ["Hello there how are you?", "Hello there         how are you?", "Hello There How are you?"];
var term = "there how";
var rx = new RegExp("(?<!\\S)" + term.replace(/ /g, "\\s+") + "(?!\\S)", "i");
for (var i=0; i<ss.length; i++) {
var m = ss[i].match(rx) || "";
console.log(m[0]);
}

关于python - 如何使用正则表达式在具有特定规则的字符串中查找子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58080713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com