matching parentheses in python regular expression

前端 未结 3 515
礼貌的吻别
礼貌的吻别 2020-12-21 08:05

I have something like

store(s)

ending line like 1 store(s) .. I want to match , it using python regular expression.

I

相关标签:
3条回答
  • 2020-12-21 08:32

    In more or less direct reply to your comment

    Try this

    import re
    s = '1 stores(s)'
    if re.match('store\(s\)$',s):
        print('match')
    

    The solution is to use re.search instead of re.match as the latter tries to match the whole string with the regexp while the former just tries to find a substring inside of the string that does match the expression.

    0 讨论(0)
  • 2020-12-21 08:40

    have you considered re.match('(.*)store\(s\)$',text) ?

    0 讨论(0)
  • 2020-12-21 08:41

    Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default)

    Straight from the docs, but it does come up alot.

    0 讨论(0)
提交回复
热议问题