RegExps – useful, but sometimes are a nightmare…

RegExps – useful, but sometimes are a nightmare…

Mar 09
RegExps – useful, but sometimes are a nightmare…

If you are a web developer (even only a developer) you should probably know the Regular Expression. It’s simple amazing what you can check with a simple string… but sometimes the RegExp could become a nightmare.

Regexp Scientist

Why I say that? Simple, there’re a lot of kind of RegExps, every RegExp checks something, f.e.: E-Mail addresses, usernames, valid URLs, valid URLs with image, valid password, barcodes, etc…

Every RegExp check something and sometimes happens that the RegExp that we need, it’s simple unfoundable on network. So the only way is, with a lot of patience, build a new one.

Luckily, the web is full, so the times that we need to build (or create?) a new one, are really rares…

In this post, I’ll quote some useful regexp founded in the net (tested in Java):

Valid Username:
"^[a-zA-z0-9]+([-.][a-zA-Z0-9]+)*$"
No check of the lenght

Valid E-Mail address:
"([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})"
Thanks to txt2re.com

Valid URL:
"((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s\"]*))"
Thanks to txt2re.com

Valid Last Name:
"^[a-zA-z]+([ '-][a-zA-Z]+)*$"
Thanks to deitel.com

Valid EAN13:
"^[0-9]{12,13}$"
If you need to check all the 13 digits, remove the “12,”

If you need to check your RegExp before use it, try this resource: rexv.org, it’s simple awesome!

Leave a Reply