NSRegularExpression行为怪异(正则表达式是正确的)

时间:2022-09-06 20:59:52

I've got this regex

我有这个正则表达式

([0-9]+)\(([0-9]+),([0-9]+)\)

that I'm using to construct a NSRegularExpression with no options (0). That expression should match strings like

我正在使用构造一个没有选项(0)的NSRegularExpression。该表达式应匹配字符串

1(135,252)

and yield three matches: 1, 135, 252. Now, I've confirmed with debuggex.com that the expression is correct and does what I want. However, iOS refuses to acknowledge my efforts and the following code

并产生三个匹配:1,135,252。现在,我已经通过debuggex.com确认表达式是正确的并且做我想要的。但是,iOS拒绝承认我的努力和以下代码

NSString *nodeString = @"1(135,252)";
NSArray *r = [nodeRegex matchesInString:nodeString options:0 range:NSMakeRange(0, nodeString.length)];
NSLog(@"--- %@", nodeString);
for(NSTextCheckingResult *t in r) {
    for(int i = 0; i < t.numberOfRanges; i++) {
        NSLog(@"%@", [nodeString substringWithRange:[t rangeAtIndex:i]]);
    }
}

insists in saying

坚持说

--- 1(135,252)
135,252
13
5,252
5
252

which is clearly wrong.

这显然是错的。

Thoughts?

2 个解决方案

#1


7  

Your regex should look like this

你的正则表达式应该是这样的

[NSRegularExpression regularExpressionWithPattern:@"([0-9]+)\\(([0-9]+),([0-9]+)\\)" 
                                          options:0 
                                            error:NULL];

Note the double backslashes in the pattern. They are needed because the backslash is used to escape special characters (like for example quotes) in C and Objective-C is a superset of C.

注意图案中的双反斜杠。它们是必需的,因为反斜杠用于转义C中的特殊字符(例如引号),而Objective-C是C的超集。


If you are looking for a handy tool for working with regular expressions I can recommend Patterns. Its very cheap and can export straight to NSRegularExpressions.

如果您正在寻找一个方便的工具来处理正则表达式,我可以推荐模式。它很便宜,可以直接输出到NSRegularExpressions。

#2


2  

Debuggex currently supports only raw regexes. This means that if you are using the regex in a string, you need to escape your backslashes, like this:

Debuggex目前仅支持原始正则表达式。这意味着如果您在字符串中使用正则表达式,则需要转义反斜杠,如下所示:

([0-9]+)\\(([0-9]+),([0-9]+)\\)

Also note that Debuggex does not (yet) support Objective C. This probably won't matter for simple regexes, but for more complicated ones, different engines do different things. This can result in some unexpected behavior.

另请注意,Debuggex(尚未)支持Objective C.这可能对简单的正则表达无关紧要,但对于更复杂的正则表达式,不同的引擎会做不同的事情。这可能会导致一些意外行为。

#1


7  

Your regex should look like this

你的正则表达式应该是这样的

[NSRegularExpression regularExpressionWithPattern:@"([0-9]+)\\(([0-9]+),([0-9]+)\\)" 
                                          options:0 
                                            error:NULL];

Note the double backslashes in the pattern. They are needed because the backslash is used to escape special characters (like for example quotes) in C and Objective-C is a superset of C.

注意图案中的双反斜杠。它们是必需的,因为反斜杠用于转义C中的特殊字符(例如引号),而Objective-C是C的超集。


If you are looking for a handy tool for working with regular expressions I can recommend Patterns. Its very cheap and can export straight to NSRegularExpressions.

如果您正在寻找一个方便的工具来处理正则表达式,我可以推荐模式。它很便宜,可以直接输出到NSRegularExpressions。

#2


2  

Debuggex currently supports only raw regexes. This means that if you are using the regex in a string, you need to escape your backslashes, like this:

Debuggex目前仅支持原始正则表达式。这意味着如果您在字符串中使用正则表达式,则需要转义反斜杠,如下所示:

([0-9]+)\\(([0-9]+),([0-9]+)\\)

Also note that Debuggex does not (yet) support Objective C. This probably won't matter for simple regexes, but for more complicated ones, different engines do different things. This can result in some unexpected behavior.

另请注意,Debuggex(尚未)支持Objective C.这可能对简单的正则表达无关紧要,但对于更复杂的正则表达式,不同的引擎会做不同的事情。这可能会导致一些意外行为。