So I have a string that looks like this:
"greeting = 'hello;'"
and I try to match this regexp to it:
"**'.*;.\*'"**
so that it detects that there is a ; char inside of two apostrophes on both sides. When I simply try this out on regex101.com it works, but when I copy paste this to python it doesn't -
g = re.match("'**.\*;.\***'", "greeting = 'hello;'")
returns None
but simply putting '**.\*;.***'
into regex on regex101 and greeting = 'hello;'
into the text field there works (matches 'hello;'
as intended).
How is this possible? How to fix this, so that it is correctly detected in Python also?
CodePudding user response:
re.match
has to match from the beginning of the string.
re.search
will match anywhere in the string.
CodePudding user response:
I think the issue is with your pattern for the regex, here's a good guide on the syntax: regex Here's my example that finds your word:
import re
string = "'hello;'"
print(string)
if re.match("'\w ;'", string):
print(string)
'hello;'
'hello;'