I’m working on a regex to match this set of numbers:
xxxx xxx xxxx
01 xxx xxxx
234 1 xxx xxxx
xxxx-xxx-xxxx
xxxxxxxxxxx
234 xxx xxx xxxx
234(xxx)xxx-xxxx
234(xxx)xxx xxxx
234(xxx)xxx xxxx
234(xxx) xxx xxxx
234(xxx)xxx-xxxx
234xxxxxxxxxx
234xxxxxxxxxx
The rules are:
- At most 11 digits long (any number combo, excluding the static parts)
- Optional (234) or ( 234)
- Optional 1 or 01 or 234 1
- Optional bracketing as specified above.
It’s to validate the Nigerian phone numbering system. I have searched far and wide and haven’t seen any good solutions so far.
I have this expression:
/^(\ )?234[0-9]*?.*/gm
but it doesn’t match the ones without (234) properly.
Specifically:
xxxx xxx xxxx
01 xxx xxxx
xxxx-xxx-xxxx
xxxxxxxxxxx
How can I make this work? I’m kinda new to regex, would appreciate any assistance I can get.
CodePudding user response:
With something as simple as:
$output = trim(filter_var($input, FILTER_SANITIZE_NUMBER_INT), ' ');
You will only be left with digits. Now you might want to remove several optional starts:
$output = preg_replace('/^0|^01|^234|^2341/', '', $input);
And finally you may want to check if the length is valid.
This does not invalidate a number like ' 234(123) (456) 7890', but should it? After all, you can only dial numbers.
CodePudding user response:
You can use
^(?:(?:(?:\ ?234(?:\h1)?|01)\h*)?(?:\(\d{3}\)|\d{3})|\d{4})(?:\W*\d{3})?\W*\d{4}$
See the regex demo. Details:
^
- start of a string(?:(?:(?:\ ?234(?:\h1)?|01)\h*)?(?:\(\d{3}\)|\d{3})|\d{4})
:(?:(?:\ ?234(?:\h1)?|01)\h*)?
- an optional occurrence of(?:\ ?234(?:\h1)?|01)
- an optional234
optionall followed with a horizontal whitespace and1
, or01
\h*
- zero or more horizontal whitespaces
(?:\(\d{3}\)|\d{3})|
-(
, three digits,)
or three digits, or\d{4}
- four digits
(?:\W*\d{3})?
- an optional sequence of zero or more non-word chars and then 3 digits\W*
- zero or more non-word chars\d{4}
- four digits$
- end of the string.
To match the phone numbers anywhere in a longer string use
(?:(?:(?:\ ?234(?:\h1)?|01)\h*)?(?:\(\d{3}\)|\d{3})|\d{4})(?:\W*\d{3})?\W*\d{4}(?!\d)
See the regex demo.