Chaitanya
Chaitanya

Reputation: 379

Need Regex for a text box so that it accepts first letter a character and remaining as digits

Hi all i need a regex that accepts first letter as character and the remaining should be digits.

Even spacing is not allowed too..

Possible cases : a123, abc123, xyz123 and so on ...
Unacceptable : 123abc,1abc12, a 123 and so on..

i tried some think like this but i am not sure this works so can any one help me..

"[A-Z][a-z]\d{0,9}"

Upvotes: 0

Views: 276

Answers (5)

NobRuked
NobRuked

Reputation: 593

I would highly recommend you do not limit yourself to ASCII as most of the other answers for this question do.

Using character classes, which I recommend, you should use:

^[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Pc}]\d+$

See the reference for ^ and $.

Upvotes: 0

Ericpoon
Ericpoon

Reputation: 267

[A-Z]|[a-z]{1,}\d{1,}

But as you mentioned, possible cases should be: a123, b321, z4213213, but not abc123. Right?

So regExp shoud be [A-Z]|[a-z]\d{1,} .

Upvotes: 0

ojlovecd
ojlovecd

Reputation: 4892

you probably need this:

"[a-zA-Z]+\d+"

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

^[A-Za-z]+[0-9]+$

matches one or more ASCII letters, followed by one or more ASCII digits. If digits are optional, use [0-9]* instead.

If you want to also allow other letters/digits than just ASCII, use

^\p{L}+\p{D}+$

Upvotes: 2

Developer
Developer

Reputation: 8636

How about this expression [A-Za-z]\w*

Upvotes: 0

Related Questions