mardi 27 septembre 2016

Regex for particular pattern with spaces

The pattern i need to check is "1w 1d 1h 1m 1s", with some restrictions, for example there can be only 1 to 3 numbers in each section, any section can be missing, there can be any numbers or spaces and other white space characters between the parts, the parts must be in order. I wrote a basic regex for it:

^((?:\s*\d{1,3}[wW]\s*)?(?:\s+\d{1,3}[dD]\s*)?(?:\s+\d{1,3}[hH]\s*)?(?:\s+\d{1,3}[mM]\s*)?(?:\s+\d{1,3}[sS]\s*)?)$

Now the problem - I got stuck, because if I write "1d 1h" - it wont pass, because it requires a space before "1d", but I cant not have that space, "1w1d" should be incorrect. Using regex conditions is not really an option.

Aucun commentaire:

Enregistrer un commentaire