2016-02-09 84 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答案

再次感謝所有

相關問題