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);