Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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,我正在尝试匹配电子邮件中的正则表达式。如果电子邮件上说“需要更新SRT1000”,则正则表达式需要匹配。我有我的代码如下,但它不工作。有人能看看这个,告诉我这里出了什么问题吗 def status_update_regex(email): email = email.lower() need_sr_update_regex = re.compile('(looking|want|need|seek|seeking|request|requesting)([^/./!/?/,/"]

我正在尝试匹配电子邮件中的正则表达式。如果电子邮件上说“需要更新SRT1000”,则正则表达式需要匹配。我有我的代码如下,但它不工作。有人能看看这个,告诉我这里出了什么问题吗

def status_update_regex(email):
    email = email.lower()
    need_sr_update_regex = re.compile('(looking|want|need|seek|seeking|request|requesting)([^/./!/?/,/"]{0,10})(status|update)(^.{0,6})(^srt[0-9]{4})')
    if need_sr_update_regex.search(email) != None:
        return 1
    else:
        return 0

不要将
^
放在组中,它试图匹配开头。另外,不需要额外的
/

'(looking|want|need|seek|seeking|request|requesting)([^/.!?,"]{0,10}(status|update)(.{0,6})(srt[0-9]{4})' 
  • 您没有在单词之间添加空格
    \s
  • 您没有字符串上的
(looking | want | need | seek | seek | seek | request | request | | | request | | | | | | | | | | | | | | | | | | | | | | | |


我能给任何尝试正则表达式匹配的人的最好提示是使用

测试他们的解决方案。简单地说,匹配“需要在SRT1000上更新”的正则表达式是“需要在SRT1000上更新”“。因为你显然需要它来匹配更多的内容,所以它可能有助于描述正则表达式的每个部分应该做什么,因为我很难理解它的某些部分。你可能需要在正则表达式中大写
srt
吗?你的正则表达式有很多问题-你应该使用正则表达式站点进行匹配测试,以确保你正确地构建了表达式。只是一个提示:移动你的
re.compile
在功能之外。每次调用
status\u update\u regex()
(^.{0,6})
时编译表达式是没有意义的。此时无法匹配字符串的开头。作为方括号内的第一个字符,插入符号表示“不匹配”以下字符。看,我知道,我显然保留了这个。OP在括号内的组中也有
^
。我的评论是为了向OP解释这一点。很抱歉造成混淆。:-)