I'm building up a library of filters for a validation class in PHP, some of them using regular expressions. I have a lot of filters in mind, but I also don't want to potentially miss any. What do you most often use regular expressions to check? What are some of the not-so-common things that you've had to check that would still be useful in a library? Note: I'm not looking for the actual regex code, just what you use it for.
Regex should be strongly tested with their expected use cases. Hence, it may be difficult to develop a complete and general library. I would aim for a library of functions you know you need now. Then add to this list later, when you have proper test cases.
That said, here are some common use cases:
Numeric Data
Phone numbers
Dates
Zip codes
SSN
so you're looking for the type regular expressions we use for validating?
telephone (various international formats), postal code, zip code, credit card #s, email, dates, digits, ssn, urls (http, ftp, ...)
In addition to Nescio's answers...
- Passwords
- Email addresses
- Disallowing characters various charters in text fields like non-alphanumeric characters
SQL injection attack patterns
'[\s]*--
Password Strength
((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,255})
The majority of my RE use is fixing up data given to me by various sources into a standardized format. A lot of exporting excel docs as CSV or tab delimited and then running through a bunch of RE transformations in TextPad.
My main uses for regular expression are:
- pulling apart text
- selecting lines in input
- validating formats
- analyzing/sanitizing input
- parsing
- providing expansive customization (allowing "configurable configurations", shortcuts,...)
A number of these things overlap. But it all has to do with human input. Machine readable and human readable are two different things. Regular expressions help us deal with human-oriented (that we know something about) stuff without needing a complete grammar.