Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regular expression zero or more occurrences

I can\'?t (.*)

My regex is of the above form. But my match object doesn't match if the string given to it ends after t

re.compile(r'I can\'?t (.*)').match(str)

If str = "I cant", it doesn't work. But if str = "I can't use", it works (match returns something).

like image 751
Sreecharan Desabattula Avatar asked Feb 03 '26 02:02

Sreecharan Desabattula


1 Answers

You left a mandatory space after the t. Remove it and you'll be fine:

I can\'?t(.*)

Also note that the brackets are only useful if you want to use the content of the first capturing group, if not you can safely remove them.

like image 125
Robin Avatar answered Feb 05 '26 15:02

Robin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!