Guilherme David da Costa
Guilherme David da Costa

Reputation: 2368

How to validate false on only numbers via regex?

I need to validate a domain registration as it cannot be made of numbers only and the validation need to fit in the way my algorithm works, that is to return full domain name(without the .com, .net, etc extensions) if it is correct.

I've tryed a few expressions with no success:

^[^0-9]+$ # that one wont let he type a number
^\w[^0-9]+\w$ # that wont work too

Can someone help me?

Upvotes: 1

Views: 367

Answers (3)

Chriszuma
Chriszuma

Reputation: 4557

So, just to be clear, you want it to return False on strings that contain ONLY numbers? Try this:

EDIT 4 Based on literal interpretation of the question, this is all you need:

\D

That will match True for any string containing a character that's not a number.

However, based on the fact that you want to match URLs, you probably want something more like this:

^\w*[a-zA-Z_]\w*$

That will match any string containing alphanumerics and _s, as long as it contains at least one letter or _.

Upvotes: 3

FailedDev
FailedDev

Reputation: 26920

(?=.*\D)

Use lookahaed. This will fail if the string consists of digits only.

Upvotes: 1

Yaakov Shoham
Yaakov Shoham

Reputation: 10538

You want al least one character that isn't digit. So: ^.*[^0-9].*$.

If you want just letters (but not whole .), you need to lookahead: ^\w*(?![0-9])\w\w*$, means: no digit, but letter.

http://www.regular-expressions.info/lookaround.html

Upvotes: 1

Related Questions