Python split string on regex

后端 未结 3 2030
忘了有多久
忘了有多久 2020-12-11 15:01

I\'m trying to split a string using a regular expression.

Friday 1Friday 11 JAN 11

The output I want to achieve is

[\'Fr         


        
相关标签:
3条回答
  • 2020-12-11 15:13

    The problem is the capturing parentheses. This syntax: (?:...) makes them non-capturing. Try:

    p = re.compile(r'((?:Friday|Saturday)\s*\d{1,2})')
    
    0 讨论(0)
  • 2020-12-11 15:18

    You can also use 're.findall' function.

    \>>> val  
    'Friday 1Friday 11 JAN 11 '  
    \>>> pat = re.compile(r'(\w+\s*\d*)')  
    \>>> m=re.findall(pat,val)  
    \>>> m  
    ['Friday 1', 'Friday 11', 'JAN 11']
    
    0 讨论(0)
  • 2020-12-11 15:26
    p = re.compile(r'(Friday\s\d+|Saturday)')
    
    0 讨论(0)
提交回复
热议问题