How to use regex with optional characters in python?

后端 未结 5 698
离开以前
离开以前 2020-12-01 12:12

Say I have a string

\"3434.35353\"

and another string

\"3593\"

How do I make a single regular expression

5条回答
  •  余生分开走
    2020-12-01 12:35

    use (?:|). replace with the string to make optional. I tested in python shell and got the following result:

    >>> s = re.compile('python(?:3|)')
    >>> s
    re.compile('python(?:3|)')
    >>> re.match(s, 'python')
    
    >>> re.match(s, 'python3')
    ```
    

提交回复
热议问题