简体   繁体   English

正则表达式在iOS上无法正常工作

[英]Regex not working correctly on iOS

Can someone please explain the meaning of following regex : 有人可以解释以下正则表达式的含义吗?

^[+]?[0-9]{0,4}[ ]?(\\([0-9]{1,6}\\))?[0-9 ()/-]{4,}$ ^ [+]?[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 这不允许我验证(0049)(0)151-544 / 187 29或(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 (). 我尝试调试,我的理解是,数字有一个问题,它有2个开括号和闭括号()。

If I make (0049) (0)151-544/187 29 as (0049) 151-544/187 29 , this works. 如果我将(0049)(0)151-544 / 187 29设为(0049)151-544 / 187 29 ,则此方法有效。

Can someone please help. 有人可以帮忙吗?

Your regex ^[+]?[0-9]{0,4}[ ]?(\\([0-9]{1,6}\\))?[0-9 ()/-]{4,}$ expressed as 您的正则表达式^[+]?[0-9]{0,4}[ ]?(\\([0-9]{1,6}\\))?[0-9 ()/-]{4,}$表示为

在此处输入图片说明

If You need group1 multiple times add * to that group 如果您需要多次使用group1,请在该组中添加*

<code>在此处输入图片描述</ code>

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 如果不起作用,请根据需要替换星号

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. 顺便说一句-这是参考文档正常工作的一个例子,因为在此问题之前,我还没有尝试过使用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. -我的解释直接来自下面参考链接的表1和2。

^ - 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-9] -[模式]匹配模式中的任何一个字符。

{0,4} - Match between 0 and 4 times. {0,4} -匹配0到4次。 Match as many times as possible, but not more than 4. 匹配次数尽可能多,但不超过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. (\\([0-9]{1,6}\\)) -(...)捕获括号。 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. [0-9] -[模式]匹配模式中的任何一个字符。

{1,6} - Match between 1 and 6 times. {1,6} -匹配1到6次。 Match as many times as possible, but not more than 6. 匹配次数尽可能多,但不超过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. [0-9 ()/-] -[图案]匹配图案中的任何一个字符。

{4,} - Match at least 4 times. {4,} -至少匹配4次。 Possessive Match. 所有格。

$ - Match at the end of a line. $ -在行尾匹配。 See NSRegularExpressionAnchorsMatchLines and the m character expression in Table 4. 请参阅NSRegularExpressionAnchorsMatchLines和表4中的m字符表达式。

See the apple documentation on NSRegularExpression Class Reference for further explanations of the metacharacters and operators. 有关元字符和运算符的进一步说明,请参见NSRegularExpression类参考上Apple文档

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. 我发现,escapedPatternForString方法可以产生一个模式样本,从而允许从一个人的期望输出中对转义的模式字符串进行反向工程。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM