0

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!

Alain D'Ettorre
  • 134
  • 1
  • 4
  • 12
  • You probably need to chain lookaheads `^(?=.*?[A-Z])(?=.*?`...`)`... – bobble bubble Apr 14 '17 at 16:30
  • I can't seem to make it work anyway. I've tried `(?=[a-z]+)(?=[0-9]+)` to match ONLY a string having at least 1 lowercase letter and at least 1 number, with no further restriction (it can have any other char but must have those) but it seems to fail as it doesn't match like "test string 000". Please help. – Alain D'Ettorre Apr 15 '17 at 10:08
  • Try like this:[`^(?=[^a-z]*[a-z])(?=\D*\d).*`](https://regex101.com/r/R6oklP/1) – bobble bubble Apr 15 '17 at 13:52
  • You're right, it works now! Thank you very much. – Alain D'Ettorre Apr 22 '17 at 05:41

0 Answers0