2

Can someone please explain the meaning of following regex :

^[+]?[0-9]{0,4}[ ]?(\([0-9]{1,6}\))?[0-9 ()/-]{4,}$

This does not allow me to validate (0049) (0)151-544/187 29 or (0049)(0)15154418729

I tried to debug and my understanding says that there is an issue with the number which has 2 open and closed brackets ().

If I make (0049) (0)151-544/187 29 as (0049) 151-544/187 29 , this works.

Can someone please help.

XYZ
  • 597
  • 1
  • 7
  • 19

3 Answers3

6

Your regex ^[+]?[0-9]{0,4}[ ]?(\([0-9]{1,6}\))?[0-9 ()/-]{4,}$ expressed as

enter image description here

If You need group1 multiple times add * to that group

<code>enter image description here</code>

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
1

The above regex acceps only numbers within a single pair braces ie,() if you need multiple () to accpet add the asterisk(*)symbol before after the closing bracket

use the below regular expression instead.

^[+]?[0-9]{0,4}[ ]?(\([0-9]{1,6}\)*)?[0-9 ()/-]{4,}$

if its not worked replace the asterisk as your need

LML
  • 1,659
  • 12
  • 29
1

Can someone please explain the meaning of following regex

See my decomposition of the string you submitted for review. BTW - This is a case in point that reference docs work because before this question I had not tried to use the NSRegularExpression. Thanks for the opportunity to learn something new.

^[+]?[0-9]{0,4}[ ]?(\([0-9]{1,6}\))?[0-9 ()/-]{4,}$

Note - My explanations are pulled directly from Table 1 and 2 of the reference link below.

^ - Match at the beginning of a line

[+] - [pattern] Match any one character from the pattern.

? - Match zero or one times. Prefer one.

[0-9] - [pattern] Match any one character from the pattern.

{0,4} - Match between 0 and 4 times. Match as many times as possible, but not more than 4.

[ ] - Match any one character from the pattern (in this case its a single whitespace)

? - Match zero or one times. Prefer one.

(\([0-9]{1,6}\)) - (...) Capturing parentheses. Range of input that matched the parenthesized subexpression is available after the match

Within that subexpression we see:

\ - Treat the following character as a literal, suppressing any special meaning. Backslash escaping in substitution text is only required for '$' and '\', but may be used on any other character without bad effects.

followed by ( which means to treat the opening parenthesis as a literal

[0-9] - [pattern] Match any one character from the pattern.

{1,6} - Match between 1 and 6 times. Match as many times as possible, but not more than 6.

still within the subexpression we see:

\ - Treat the following character as a literal, suppressing any special meaning. Backslash escaping in substitution text is only required for '$' and '\', but may be used on any other character without bad effects.

followed by ) which means to treat the closing parenthesis as a literal

? - Match zero or one times. Prefer one. (This seems to be the problem, you are only matching zero or once here)

[0-9 ()/-] - [pattern] Match any one character from the pattern.

{4,} - Match at least 4 times. Possessive Match.

$ - Match at the end of a line. See NSRegularExpressionAnchorsMatchLines and the m character expression in Table 4.

See the apple documentation on NSRegularExpression Class Reference for further explanations of the metacharacters and operators.

Other note on \:

\ - Quotes the following character. Characters that must be quoted to be treated as literals are * ? + [ ( ) { } ^ $ | \ . /

Interesting Note:

I found that the escapedPatternForString method can produce a pattern sample allowing one to reverse engineer an escaped pattern string from one's desired output. It doesn't perfectly output the pattern but does help with the metacharacters..

NSString *sample = @"(0049) (0)151-544/187 29";
NSString *pattern = [NSRegularExpression escapedPatternForString:sample];

//Pattern sample: \(0049\) \(0\)151-544\/187 29
NSLog(@"Pattern sample: %@", pattern);
Tommie C.
  • 12,895
  • 5
  • 82
  • 100