Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different regular expressions in one?

I want to put this regexs into one regex, but dont now how:

/^([0-9]{2,4})\-([0-9]{6,7})$/
/^\*([0-9]{4})$/

so the string can get XX(XX)-XXXXXX(X) or *XXXX

Thanks.

like image 771
Nir Avatar asked May 28 '11 14:05

Nir


1 Answers

to merge two regular expressions A and B, do:

/(A|B)/

This may change the order of one set of capturing parentheses. Also note that parentheses may not be needed, e.g. A|B may work in your simple case.

In other languages which support regular expressions with named captures, you can use those.

like image 181
ninjagecko Avatar answered Oct 28 '22 11:10

ninjagecko