regex – Python Regular Expressions for a question mark

regex – Python Regular Expressions for a question mark

Backslash before question mark means literally match a question mark

?

Also, putting a question mark into a character class will mean its matched literally rather than having its typical 0 or 1 of the previous meaning

[?]

Thus:

bcd[?]
bcd?

Will both match data that looks like:

abcd?efg
 ^^^^

If you want to match data that is just a question mark and nothing else, use the start ^ and end $ markers:

^?$

Consider though that it may be faster not to use regex and just do a simple string contains check for the presence of a question mark if thats literally all youre doing, and dont require complex pattern matching and value capture

regex – Python Regular Expressions for a question mark

Leave a Reply

Your email address will not be published. Required fields are marked *