Khaled Hasan Khaled Hasan -4 years ago 153
Java Question

regx contains one or more phone number

I need a regular expression that will match one or more phone numbers that can be separated by semicolons (

;
) and the number length can be 7 or more characters
0-9
and
+
,
-
,
_
.

Like the following:

1234567
1234567;1234567
1234567;+0591234567
1234567777;1234567777;1234567891


I've tried the following regular expression, but it failed:

^[0-9.-_+]{7,}(;[0-9.-_+]{7,})*$

Answer Source

It's almost correct just you need to use \ (back slash) before the characters which are used in regexp. Try it-

^[0-9.\-_+]{7,}(;[0-9.\-_+]{7,})*$

UPDATE:

It's more perfect than the previous-

(?<=\;|\A)[\+\-\_]?\d{7,}(?=\;|\Z)

You may try it here. Credit goes to @sudoman's comment.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download