Regex Apostrophe how to match?

后端 未结 2 647
北荒
北荒 2021-01-19 08:16

I want to add to this rule match on Apostrophe \'

rule = re.compile(r\'^[^*$<,>?!]*$\')

I have tried:

2条回答
  •  既然无缘
    2021-01-19 09:07

    You have to escape the apostrophe, otherwise it will be counted as the end of the raw string:

    rule = re.compile(r'^[^*$<,>?!\']*$')
    

    Or, you can use " to surround your string, which is perfectly valid in python:

    rule = re.compile(r"^[^*$<,>?!']*$")
    

提交回复
热议问题