Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何执行正则表达式检查子字符串列表是否出现在字符串之前?_Python_Regex - Fatal编程技术网

Python 如何执行正则表达式检查子字符串列表是否出现在字符串之前?

Python 如何执行正则表达式检查子字符串列表是否出现在字符串之前?,python,regex,Python,Regex,我试着搜索了一下,但没有找到具体的答案 我想根据前面出现的字符来确定要使用的字符串 例如,我喜欢做的是: def check_if_char_appears(input_string): substring_list = ['aa', 'bb', 'dd', 'ef'] for i in substring_list: if i appears right before 'cake': return True return False

我试着搜索了一下,但没有找到具体的答案

我想根据前面出现的字符来确定要使用的字符串

例如,我喜欢做的是:

def check_if_char_appears(input_string):
    substring_list = ['aa', 'bb', 'dd', 'ef']
    for i in substring_list:
        if i appears right before 'cake':
           return True
    return False
结果: 条件1:

input_string = 'aacake'
check_if_char_appears(input_string)
是真的

条件2:

input_string = 'aakkcake'
check_if_char_appears(input_string)
是假的

发现java解决方案可以执行函数“如果我出现在“cake”之前:


str.matches(“i*(?)用于像您这样的简单情况

substring_list = ['aa', 'bb', 'dd', 'ef']
for i in substring_list:
    if "{}cake".format(i) in input_string:
        return True

对于像你这样的简单案例

substring_list = ['aa', 'bb', 'dd', 'ef']
for i in substring_list:
    if "{}cake".format(i) in input_string:
        return True

python()中有一个regex模块

这应该做完全相同的事情,只是与

import re
m = re.search("i*(?<!\\.)cake.*", your_string)
for match in m:
  print(m)
重新导入

m=re.search(“i*(?python中有一个正则表达式模块()

这应该做完全相同的事情,只是与

import re
m = re.search("i*(?<!\\.)cake.*", your_string)
for match in m:
  print(m)
重新导入
m=使用正则表达式重新搜索(“i*(?)

matches = re.match(r'.*(aa|bb|dd|ef)cake.*', your_str)
if matches:
    # do whatever you want
如果您在
蛋糕之后什么都不想要

matches = re.match(r'.*(aa|bb|dd|ef)cake', your_str)
if matches:
    # do whatever you want
使用正则表达式

matches = re.match(r'.*(aa|bb|dd|ef)cake.*', your_str)
if matches:
    # do whatever you want
如果您在
蛋糕之后什么都不想要

matches = re.match(r'.*(aa|bb|dd|ef)cake', your_str)
if matches:
    # do whatever you want

你可以在这里使用正则表达式。你可以让正则表达式像这样在移动中

substring_list = ['aa', 'bb', 'dd', 'ef']
if re.match(r"({})cake".format("|".join(substring_list)), input_string):
    return True
长答覆:

def check_if_char_appears(input_string):
    substring_list = ['aa', 'bb', 'dd', 'ef']
    sub_string_re = "|".join(substring_list) # 'aa|bb|dd|ef'
    re_string = r"({})cake".format(sub_string_re) # (aa|bb|dd|ef)cake
    if re.match(re_string, input_string):
        return True
    return False


input_string = 'aacake'
print(check_if_char_appears(input_string))

input_string = 'aakkcake'
print(check_if_char_appears(input_string))
输出:


你可以在这里使用正则表达式。你可以让正则表达式像这样在移动中

substring_list = ['aa', 'bb', 'dd', 'ef']
if re.match(r"({})cake".format("|".join(substring_list)), input_string):
    return True
长答覆:

def check_if_char_appears(input_string):
    substring_list = ['aa', 'bb', 'dd', 'ef']
    sub_string_re = "|".join(substring_list) # 'aa|bb|dd|ef'
    re_string = r"({})cake".format(sub_string_re) # (aa|bb|dd|ef)cake
    if re.match(re_string, input_string):
        return True
    return False


input_string = 'aacake'
print(check_if_char_appears(input_string))

input_string = 'aakkcake'
print(check_if_char_appears(input_string))
输出:



你需要使用正则表达式吗?否则,只需将每个组合附加到“cake”并搜索结果词“aacake”、“bbcake”等。有任何解决方案对你有效吗?您好,我尝试了所有答案,所有答案都很有效。最后我选择了不带循环的正则表达式。谢谢!你需要使用正则表达式吗?否则,只需附加each组合到“cake”并搜索结果词“aacake”、“bbcake”等。有任何解决方案对您有效吗?您好,我尝试了所有答案,所有答案都很有效。最后我选择了不带循环的正则表达式。谢谢!足够简单,但我认为纯正则表达式方法更有效:您不必生成所有这些字符串&这个循环是由专门的正则表达式引擎完成的。谢谢你的解释。我也认为正则表达式是这里最好的伙伴。但是非正则表达式解决方案在简单的时候总是很好的。性能有时不是唯一的因素。bozos也有可维护性:)(在这种情况下,正则表达式不太合适)它也非常适合OP尝试。足够简单,但我认为纯正则表达式的方法更有效:你不必生成所有的字符串&循环是由专门的正则表达式引擎完成的。谢谢你的解释。我也认为正则表达式是这里最好的伙伴。但非正则表达式的解决方案在简单的时候总是好的。性能不是最好的e有时是唯一的因素。bozos也有可维护性:)(在这种情况下,regex不太适合)它也很好地支持OP尝试。它不匹配特定的字符串,如OP想要的。它不匹配特定的字符串,如OP想要的。谢谢。这工作很好,没有循环似乎很快谢谢。这工作很好,没有循环似乎很快