Python 用于匹配具有不同组合的字符串的正则表达式
我正在尝试使用python将字符串与以下不同的组合进行匹配 (此处x是长度4的数字) 这里的第一部分和最后一部分是静态的,第二部分可以有上面所示的任何组合,就像有时几天之间用“,”或“-”分隔一样 我是正则表达式的新手,我在谷歌上搜索了正则表达式的工作原理,我可以对上面的表达式进行重新编译,比如将最后一部分与Python 用于匹配具有不同组合的字符串的正则表达式,python,regex,r,Python,Regex,R,我正在尝试使用python将字符串与以下不同的组合进行匹配 (此处x是长度4的数字) 这里的第一部分和最后一部分是静态的,第二部分可以有上面所示的任何组合,就像有时几天之间用“,”或“-”分隔一样 我是正则表达式的新手,我在谷歌上搜索了正则表达式的工作原理,我可以对上面的表达式进行重新编译,比如将最后一部分与RE.compile('(\d{4})-(\d{4})$)匹配,第一部分与RE.compile('[w{124; w]')匹配 我试图匹配第二部分,但没有成功 new_patt = re.c
RE.compile('(\d{4})-(\d{4})$)
匹配,第一部分与RE.compile('[w{124; w]')
匹配
我试图匹配第二部分,但没有成功
new_patt = re.compile('(([a-zA-Z]{3}))([,-]?)(([a-zA-Z]{3})?))
我如何才能做到这一点?您可以一次完成所有任务:
^W\|(?:\w{3}[-,]){0,2}\w{3}\|(?:\d{4}[-]?){2}$
使用您可以一次完成所有任务:
^W\|(?:\w{3}[-,]){0,2}\w{3}\|(?:\d{4}[-]?){2}$
对于,这里有一个正则表达式应该可以工作:
pat = re.compile('^W\|(mon|tue|wed|thu|fri|sat|sun)(-(mon|tue|wed|thu|fri|sat|sun))?(,(mon|tue|wed|thu|fri|sat|sun)(-(mon|tue|wed|thu|fri|sat|sun))?)?\⎪\d{4}-\d{4}$', re.IGNORECASE)
首先请注意如何忽略大小写以处理小写和大写。除了开头的静态文本和结尾的数字外,此正则表达式还匹配一周中的一天,后面是可选的破折号+一周中的一天,后面是可选的序列,其中包含、
和上一个序列
"^W\|(mon|tue|wed|thu|fri|sat|sun)(-(mon|tue|wed|thu|fri|sat|sun))?(,(mon|tue|wed|thu|fri|sat|sun)(-(mon|tue|wed|thu|fri|sat|sun))?)?\|\d{4}-\d{4}$"i
^ assert position at start of the string
W matches the character W literally (case insensitive)
\| matches the character | literally
1st Capturing group (mon|tue|wed|thu|fri|sat|sun)
2nd Capturing group (-(mon|tue|wed|thu|fri|sat|sun))?
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
- matches the character - literally
3rd Capturing group (mon|tue|wed|thu|fri|sat|sun)
4th Capturing group (,(mon|tue|wed|thu|fri|sat|sun)(-(mon|tue|wed|thu|fri|sat|sun))?)?
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
, matches the character , literally
5th Capturing group (mon|tue|wed|thu|fri|sat|sun)
6th Capturing group (-(mon|tue|wed|thu|fri|sat|sun))?
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
- matches the character - literally
7th Capturing group (mon|tue|wed|thu|fri|sat|sun)
\| matches the character | literally
\d{4} match a digit [0-9]
Quantifier: {4} Exactly 4 times
- matches the character - literally
\d{4} match a digit [0-9]
Quantifier: {4} Exactly 4 times
$ assert position at end of the string
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
这里有一个正则表达式应该可以工作:
pat = re.compile('^W\|(mon|tue|wed|thu|fri|sat|sun)(-(mon|tue|wed|thu|fri|sat|sun))?(,(mon|tue|wed|thu|fri|sat|sun)(-(mon|tue|wed|thu|fri|sat|sun))?)?\⎪\d{4}-\d{4}$', re.IGNORECASE)
首先请注意如何忽略大小写以处理小写和大写。除了开头的静态文本和结尾的数字外,此正则表达式还匹配一周中的一天,后面是可选的破折号+一周中的一天,后面是可选的序列,其中包含、
和上一个序列
"^W\|(mon|tue|wed|thu|fri|sat|sun)(-(mon|tue|wed|thu|fri|sat|sun))?(,(mon|tue|wed|thu|fri|sat|sun)(-(mon|tue|wed|thu|fri|sat|sun))?)?\|\d{4}-\d{4}$"i
^ assert position at start of the string
W matches the character W literally (case insensitive)
\| matches the character | literally
1st Capturing group (mon|tue|wed|thu|fri|sat|sun)
2nd Capturing group (-(mon|tue|wed|thu|fri|sat|sun))?
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
- matches the character - literally
3rd Capturing group (mon|tue|wed|thu|fri|sat|sun)
4th Capturing group (,(mon|tue|wed|thu|fri|sat|sun)(-(mon|tue|wed|thu|fri|sat|sun))?)?
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
, matches the character , literally
5th Capturing group (mon|tue|wed|thu|fri|sat|sun)
6th Capturing group (-(mon|tue|wed|thu|fri|sat|sun))?
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
- matches the character - literally
7th Capturing group (mon|tue|wed|thu|fri|sat|sun)
\| matches the character | literally
\d{4} match a digit [0-9]
Quantifier: {4} Exactly 4 times
- matches the character - literally
\d{4} match a digit [0-9]
Quantifier: {4} Exactly 4 times
$ assert position at end of the string
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
谢谢你的帖子和评论 最后,我能够用正则表达式满足我的要求 给你 “^[w | w]\\|(周一、周五、周五、周四、周六、周三、周二)(周一、周五、周六、周三、周四、周四、周二、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、 我刚刚修改了Julien Spronck发布的答案
再次感谢大家感谢你们的帖子和评论 最后,我能够用正则表达式满足我的要求 给你 “^[w | w]\\|(周一、周五、周五、周四、周六、周三、周二)(周一、周五、周六、周三、周四、周四、周二、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、周四、 我刚刚修改了Julien Spronck发布的答案
再次感谢大家为什么在这上面有一个r标记?嗨,Grothendieck,在python官方文档中是这样的,在很多例子中,他们在每个模式前面都使用r.compile(r“”)作为r标记,因此表示r编程语言。建议您从帖子中删除该标记。为什么这上面会有一个r标记?嗨,Grothendieck,在python官方文档中是这样的,在许多示例中,他们在每个模式前面都使用r.compile(r“”)作为r标记,因此表示r编程语言。建议您从帖子中删除该标签。谢谢托马斯,这很容易理解。但是这个RE的工作长度也是错误的,如下所示。compile(“^W\”(?:\W{3}[-,])*“(?:\d{4}[-]?)+$”)new|pat.match('W | monsdf,thussf,fri | 1200-1300')。group()输出:'W | monsdf,thussf f,fri | 1200-1300'这里一天的长度对于monsdf,thussfThanks-Thomas,这很容易理解。但是这个RE也适用于错误的长度,如下所示:W|pat=RE.compile(r'^W\\(?:\W{3}[-,])*\(?:\d{4}[-]+$)new|pat.match('W | monsdf,thussf,fri | 1200-1300')。group()输出:“W | monsdf,thussf,fri,fri | 1200-1300”在这里,monsdf,thussf一天的长度超过3