Here is my regex so far :
^((([a-zA-Z0-9_\/-]+)[ ])+((\bPHONE_NUMBER\b)|(\b(IP|EMAIL)_ADDRESS\b))[ ]*[;]*[ ]*)+$
I would like to make at least one ;
mandatory if I found another (([a-zA-Z0-9_\/-]+)[ ])+((\bPHONE_NUMBER\b)|(\b(IP|EMAIL)_ADDRESS\b))
after the first one.
/tests/phone PHONE_NUMBER ; /tests/IP IP_ADDRESS
should match.
/tests/phone PHONE_NUMBER /tests/IP IP_ADDRESS
should not match.
How can I achieve that ?
Yup, you can do that. Use Recursive Regex for that.
^(((\s*(([\w_\/-]+)\s)((\bPHONE_NUMBER\b)|(\b(IP|EMAIL)_ADDRESS\b))\s*))(;|$)(?1)*)
https://regex101.com/r/dE2nK2/3
(?1)
is a recursive regex to repeat the regex pattern of group 1. If you want to do recursive regex for the whole string, use (?R)
, but you won't be able to use beginning anchor ^
.(;|$)
the matched regex should be ended with either ;
or the end of the string $
.\s
for whitespace instead of [ ]
.;*
and [;]*
is same.You can learn more about the recursive regex here: http://www.rexegg.com/regex-recursion.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With