我试图使用python
将字符串与以下不同的组合进行匹配(这里x是长度为4的数字)
W|MON-FRI|xxxx-xxxx
W|mon-fri|xxxx-xxxx
W|MON-THU,SAT|xxxx-xxxx
W|mon-thu,sat|xxxx-xxxx
W|MON|xxxx-xxxx
这里的第一部分和最后一部分是静态的,第二部分可以是如上所示的任何组合,就像有时候用“,”或“ - ”分隔天。
我是正则表达式的新手,我在Google上搜索正则表达式的工作方式,我能够为比特&以上表达式的部分,例如将最后一部分与re.compile('(\d{4})-(\d{4})$')
匹配,第一部分与re.compile('[w|W]')
匹配。
我试图匹配第二部分,但无法用
成功new_patt = re.compile('(([a-zA-Z]{3}))([,-]?)(([a-zA-Z]{3})?))
我怎样才能做到这一点?
答案 0 :(得分:0)
答案 1 :(得分:0)
这是一个应该有效的正则表达式:
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])
答案 2 :(得分:0)
感谢您的帖子和评论,
最后,我能够用正则表达式满足我的要求 这是
“^ [W | W] \ |(周一|太阳|周五|周四|坐在|结婚|周二| [0-6])( - (周一|周五|坐在|太阳|结婚|周四|周二| [0-6]))(,(星期一|星期五|坐|太阳|星期三|星期四|星期二| [0-6]))* \ |?(\ d {4} - \ d {4})$ “IMG
我刚刚调整了Julien Spronck发布的答案
再次感谢所有