r'^a$'
is used as complete match.
Above pattern says... a string should start with letter a
and end with letter a
.
What stops this pattern(r'^a$'
) to match string 'anna'
?
r'^a$'
is used as complete match.
Above pattern says... a string should start with letter a
and end with letter a
.
What stops this pattern(r'^a$'
) to match string 'anna'
?
a string should start with letter
a
and end with lettera
That's not the only thing the regex says: it also requires the string to have no other characters in between the initial and final letter, meaning that the only string matched by this expression is a single-character string a
.
In order to fix this, add .*?
to match "the middle" of the string:
^a.*?a$
Note that this expression no longer matches a single-character string a
, requiring at least two a
s to be there.
You're not interpreting it correctly.
A regular expression is processed left-to-right, matching parts of the input as it goes along.
^a$
means that the match starts at the beginning of the string, then has to match a
right after, then has to match the end of the string immediately after that.
It's no different from
abc
meaning that b
has to follow a
immediately, and c
has to follow b
immediately.
You're interpreting the meaning of the regular expression wrong.
r'^a$'
says a string that starts with letter "a" and ends with that same letter "a". That "a" character that is in the expression must be both the starting and ending characters in the string.
To extract strings that start and end with DIFFERENT a's, you can use r^a.*a$
. But this requires that the two a's be different. To get any string that starts with "a" and ends with "a", you can OR these two together:
r'^a$|^a.*a$'