I am trying to do the following with regex...
I have this so far http://regex101.com/r/yW1pV8 ...
.*[a-zA-Z]{2,}+.*
This seems to meet my critera except that it doesn't stop me from putting in other charactes such as $ _ ! etc...
Some correct test data is...
579 International Road
International Road
Some incorrect data is...
679
3
$£
A
Where am I going wrong?
You can use this regex /^[ A-Za-z0-9_@./#&+-]*$/.
You can use regular expressions to achieve this task. In order to verify that the string only contains letters, numbers, underscores and dashes, we can use the following regex: "^[A-Za-z0-9_-]*$".
The regex \w is equivalent to [A-Za-z0-9_] , matches alphanumeric characters and underscore.
.*
matches anything, which isn't what you want it seems. Also, you don't need the +
, since X{n,}
already means X
at least n
times. Lastly, you forgot the 0-9
part. So it looks like this will do:
[a-zA-Z0-9]{2,}
Some regex flavors have [a-zA-Z0-9]
as a pre-defined character class. For example, in Java it's \p{Alnum}
.
If you also want to allow for spaces (as per your test data), use \s
:
(?:\s*[a-zA-Z0-9]{2,}\s*)*
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