我试图使用正则表达式在单词边界的字符串输入中匹配一个子字符串,然后再匹配另一个子字符串。例如如果
string_1 = "I will give you a call in case I need some help in future"
并且如果2个子字符串是“ will”和“ in case I need”
对于字符串1应该返回true
但对于下面的字符串应返回false
string_2 = "in case I need some help I will call you"
我需要不区分大小写的匹配,并且只能使用正则表达式。
对于以下内容,它也应该返回false,因为它不包含“万一我需要”后跟“意志”
string_3 = "I will let you know"
string_4 = "I will let you know in case we need"
我看过Is there a regex to match a string that contains A but does not contain B,但无法确定我的情况如何向前/向后看。该帖子介绍了何时存在2个字符串,但不确定是否跟随另一个字符串。 需要使用python解决方案并且不能使用substring / find等,因此需要成为正则表达式
str = 'I will give you a call in case I need some help in future'
result = bool(re.search(r'^(?=.*\bwill\b)(?=.*\bin case I need\b).*', str))
print(result)
以上匹配“ will”和“ in case I need”的存在,而无需订购。我需要强制执行命令,并在一个字符串后跟另一个字符串,即“将”后跟“以防万一”。
答案 0 :(得分:0)
只需要复杂的正则表达式,因为顺序无关紧要。如果订单很重要,则要简单得多:
re.search(r'pattern1.*pattern2', string_to_search)
将寻找pattern1
,然后寻找pattern2
。