Regular Expression: +$ VS *$ VS none

^[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.


^ and $ are anchors. They do not match anything, all they do is placing the at a particular place in the input.

  • When you have ^, you tell the engine that whatever follows it must start at the beginning of the line
  • When you have $, you tell the engine that whatever precedes it must start at the end of the line
  • When you have both ^ 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.


+ 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

Tags:

Regex