Reputation: 14809
What is the difference between search pattern like [a-zA-Z][a-zA-Z]*
and [a-zA-Z]*
?
Upvotes: 1
Views: 117
Reputation: 1870
The regex [a-zA-Z][a-zA-Z]*
means that you are mandating that there should be one alpabetic character optionally followed by any number of alphabets. On the other hand, [a-zA-Z]*
means that the alphabet mandate is entirely off.
For example, your first regex matches the strings azxxx
, abccdef
but fails 2abcd
, 22
and blank strings. But the second regex can match a blank string too.
For the first regex, you may just want to say: [a-zA-Z]+
instead.
Upvotes: 2
Reputation: 63590
The first matches one [a-zA-Z]
followed by zero or more [a-zA-Z]
.
The second matches zero or more [a-zA-Z]
.
The first can also be written as [a-zA-Z]+
.
Upvotes: 8