0

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,})*$
Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34
  • 2
    Possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – miken32 Mar 01 '17 at 18:15
  • You need to be more precise about what you are trying to do, and show how you are using this regex. If I guessed your intentions then it should work: https://regex101.com/r/WP4p6F/1 – Pshemo Mar 01 '17 at 18:19

1 Answers1

2

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.

Community
  • 1
  • 1
Partharaj Deb
  • 864
  • 1
  • 8
  • 22
  • Only `-` is special in `[..]` since it can be used to create range of characters like `a-z`. We we don't need to escape `.` or `+` there. – Pshemo Mar 01 '17 at 18:21
  • And you don't need to escape `-` if it's the last character – miken32 Mar 01 '17 at 18:21
  • 1
    '+' character is used in regexp as 1 to INF repetitions, '-' is used to mention a range, '.' is used to mention any character. So you need to escape them. – Partharaj Deb Mar 01 '17 at 18:24
  • 2
    `[a+]` means `a` OR `+` characters, not `a` `aa` `aaa` because `[...]` can match SINGLE character from specified set of characters. We are *allowed* to use `\+` inside `[...]` (which I don't like because it creates only confusion like this one) but that doesn't mean this ``\`` is *required*. – Pshemo Mar 01 '17 at 18:28
  • Consider visiting http://www.regular-expressions.info/charclass.html#special to get more info about `[...]` metacharacters. – Pshemo Mar 01 '17 at 18:35