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.
There is a method for matching specific characters using regular expressions, by defining them inside square brackets. For example, the pattern [abc] will only match a single a, b, or c letter and nothing else.
The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.
Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).
Using the regular expressions.The matches() method of the String class accepts a regular expression and verifies it matches with the current String, if so, it returns true else, it returns false. The regular expression to match String which contains a digit as first character is “^[0-9]. *$”.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With