2016-02-09 53 views
1

I'n试图使用Python,以配合以下不同组合的字符串匹配不同组合的字符串正则表达式

(这里x是lenght 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的比特&件以上的表达式的像re.compile('(\d{4})-(\d{4})$')的最后一部分,并与re.compile('[w|W]')第一部分匹配。

我试图匹配第二部分,但不能与

new_patt = re.compile('(([a-zA-Z]{3}))([,-]?)(([a-zA-Z]{3})?)) 

成功我怎样才能做到这一点?

+2

为什么会出现的R标志对此有何看法? –

+0

您好格罗滕迪克,在蟒公文它是这样的,在许多示例中它们所使用的每个图案的ř盈与r.compile(R”“) – Kumar

+0

在R标签SO意味着R编程语言。建议您从帖子中移除该标签。 –

回答

0

可以一气呵成得到的一切:

^W\|(?:\w{3}[-,]){0,2}\w{3}\|(?:\d{4}[-]?){2}$ 

随着Live Demo

+0

谢谢托马斯,这很容易理解。但是,这RE正在为错误的长度,以及,如下所示 – Kumar

+0

new_pat = re.compile(R'^ W \ |(:\瓦特{3} [ - ,])* \ |(:???\ d { 4} [ - ])+ $ ') – Kumar

+0

new_pat.match(' W | monsdf,thussf,周五| 1200-1300' )基团() – Kumar

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]) 

https://regex101.com/r/dW4dQ7/1

0

感谢您的文章和评论,

我终于能满足我的要求使用正则表达式 这里是

“^ [W | W] \ |(星期一|太阳|星期五|星期四|坐|星期三|星期二| [0-6])( - (星期一|星期五|坐|太阳|星期三|星期四|星期二| [0-6]))?(, (周一|星期五|坐|太阳|星期三|星期四|星期二| [0-6]))* \ |?(\ d {4} - \ d {4})$“IMG

我只是调整了张贴朱利安Spronck答案

再次感谢所有

相关问题