holys
holys

Reputation: 14809

Why is the same criteria repeated twice in this regex?

What is the difference between search pattern like [a-zA-Z][a-zA-Z]* and [a-zA-Z]* ?

Upvotes: 1

Views: 117

Answers (2)

S.R.I
S.R.I

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

rid
rid

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

Related Questions