RegEx: match one of two patterns

I have two regular expressions: one for checking a mobile number and one for a home phone number.

Mobile Number Model:

^((07|00447|\+447)\d{9}|(08|003538|\+3538)\d{8,9})$ 

Home number:

 ((0|0044|\+44)\d{10}|(08)\d{9}|(90)\d{6}|(92)\d{6}|(437)\d{5}|(28)\d{6}|(37)\d{6}|(66)\d{6}|(82)\d{6}|(777)\d{5}|(93)\d{6})$ 

Is there a way to combine both of these expressions so that I can apply them to the Contact Number field, which would be valid if the input matched the expression?

+5
source share
3 answers

Put both regular expressions in a non-capturing group, separated by an interleave operator | .

 ^(?:((07|00447|\+447)\d{9}|(08|003538|\+3538)\d{8,9})|((0|0044|\+44)\d{10}|(08)\d{9}|(90)\d{6}|(92)\d{6}|(437)\d{5}|(28)\d{6}|(37)\d{6}|(66)\d{6}|(82)\d{6}|(777)\d{5}|(93)\d{6}))$ 
+6
source

Combine them with the pipe that he or the operator.

 ^((07|00447|\+447)\d{9}|(08|003538|\+3538)\d{8,9})|((0|0044|\+44)\d{10}|(08)\d{9}|(90)\d{6}|(92)\d{6}|(437)\d{5}|(28)\d{6}|(37)\d{6}|(66)\d{6}|(82)\d{6}|(777)\d{5}|(93)\d{6})$ 
+2
source

You may need to not capture groups with the condition | :

 ^(?:(07|00447|\+447)\d{9}|(08|003538|\+3538)\d{8,9})|(?:(0|0044|\+44)\d{10}|(08)\d{9}|(90)\d{6}|(92)\d{6}|(437)\d{5}|(28)\d{6}|(37)\d{6}|(66)\d{6}|(82)\d{6}|(777)\d{5}|(93)\d{6})$ 
+1
source

Source: https://habr.com/ru/post/1209708/


All Articles