I am more or less a regex novice. I tried to get the following conditions into a regex:
Resulted in the following regex:
^\S[\d\/. ()\-+]*\S$
But now I try to apply two more conditions:
My problem is how to merge those two conditions into the existing regex string above, as excluding + [+]{1,2} and () [(]{1} [)]{1} doesn't make much sense because I'm trying to make general statements in no particular order so that I would be able to chain it.
Use this:
^(?=\S)(?=(?:[^+]*\+){0,2}[^+]*$)(?=(?:[^(]*\()?[^(]*$)(?=(?:[^)]*\))?[^)]*$)[- .()+0-9]*[-.+()0-9]$
In the regex demo you can play with the input to check what matches and doesn't.
Explanation
^ anchor asserts that we are at the beginning of the string(?=\S) asserts that what follows is a non-space character(?=(?:[^+]*\+){0,2}[^+]*$) : two + chars at the most(?=(?:[^(]*\()?[^(]*$) : One ( at the most(?=(?:[^)]*\))?[^)]*$) : One ) at the most[- .()+0-9]* zero or more of the allowed chars[-.+()0-9] end with one of the allowed chars that is not a space$ anchor asserts that we are at the end of the stringReference
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