Sunday 3 December 2017

validation - Regex: how to match all character classes and not just one or more

I'm struggling with a regex. I'm giving the possibility to
pass human-readable set of rules to be internally translated in a regex inside a
validation function I'm writing.



I've mapped
some small patterns to some made up names like "letters" for [a-zA-Z]+ and the likes and
users can also define a list of must-have of these to say, for example, "check if string
has letters AND numbers AND hyphens".



Now, when
I get to assemble the regex, a simple [PUT_ALL_CLASSES_HERE]+
pattern with all the classes into it would match anything having class1 OR class 2 OR
... but I need this to match only a string having class1 AND class2 AND
...
in it.



The way I've brutally
solved this was to execute a separate regex match for every class name, where every
pattern was a simple
[CLASS_NAME]+.




I'm
sure there's some obvious way to do it as I don't know much about regex but please
help!

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...