user282807
user282807

Reputation: 925

Regular expression start with specific letter

I am using this ^[S-s][0-9]{4}$ to validate my string, but not working properly. my string has to be in the form of the Letter S (upper-case or lower-case) followed by 4 digits, e.g. S1234. Looks like it works for Letters above S, meaning if I enter w1234 it validates correct, but if I enter a letter below s, like a1234 it doesn’t validate. Thanks.

Upvotes: 7

Views: 60164

Answers (3)

chickensoup
chickensoup

Reputation: 351

Not answer directly the detail content of the question, but whom who end up to this question by the question's title and looking for the answer of regex to find match words begin with specific letter like : This is a Zone You should use this regex:

\bd[a-zA-Z]+

[a-zA-Z] should replace by the expected tail you want. Take a look at this link

Upvotes: 2

Regexident
Regexident

Reputation: 29562

You need to get rid of the dash:

^[Ss][0-9]{4}$

dashes within [...] denote character ranges. Thus S-s in regex would mean "every character in Unicode character table between S and s" and as those two are not adjacent, you end up with a bunch of matched chars.

Upvotes: 18

Rudism
Rudism

Reputation: 1665

[S-s] means the range of all characters between capital S and lowercase s. Try ^[Ss][0-9]{4}$ instead. Or better yet, ^s\d{4}$ with a case-insensitivity modifier (/i in many languages).

Upvotes: 0

Related Questions