In regular expressions, what is the difference between ^[a-zA-Z]+$
and ^[a-zA-Z]*$
. Also would I be able to not include it at all, either with ^[
^
and $
are anchors. They do not match anything, all they do is placing the at a particular place in the input.
^
, you tell the engine that whatever follows it must start at the beginning of the line$
, you tell the engine that whatever precedes it must start at the end of the line^
and $
you tell the engine that whatever is in between them must cover the entire line end-to-end.Now it should be easy to understand the difference between [a-zA-Z]*
and [a-zA-Z]+
placed between the anchors: the one with the asterisk allows empty lines, while the one with the plus insists on matching at least one character.
It should also be easy to understand what happens when you place only one anchor: essentially, you are letting the engine ignore the beginning (when ^
is missing) or the end of the line (when $
is missing) when looking for a match.
^[a-zA-Z]+$
[a-zA-Z]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
$ assert position at end of the string
So this will match basically any string of letters Foo
, Bar
, sdasndjkasnd
But will not match anything else like asds2
, ab c
, @23
Will not match
^[a-zA-Z]*S
[a-zA-Z]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
S matches the character S literally (case sensitive)
This will match the same but you MUST have a capital S at the end for a match.
Such as BallS
, FooS
, BarS
will match -- ASzs
will match [AS]
Balls
, Foos
Will not match though
^[a-zA-Z]+$
matches any alphabetic string (consisting of the letters a-z, large or small) of size 1 or longer. The ^ and $ characters mean "start" and "end" of the line, so they only match full lines consisting of one "word".
^[a-zA-Z]*S
is similar, only the asterisk (*) means "no or any number of" the preceding character/group. The S is just an S and matches exactly one S. Basically the whole thing matches any string that starts at the beginning of a row, contains any number of letters and ends with an S. Other things can come after the S though, the line does not have to end there since $ was not used.
+
means 1 or more
*
means 0 or more
So an empty string is found by ^[a-zA-Z]*$
, but not by ^[a-zA-Z]+$
^[a-zA-Z]$
means EXACTLY one letter in the ranges a-z and A-Z.
a+
is a
, aa
, aaa
, ..., aaa...aaa
, etc
a*
is an empty string, a
, aa
, aaa
, ..., aaa...aaa
, etc
^a$
is only a
EDIT: you can also use ^a?$
to find 0 or 1 occurence of a
, so either an empty string or a