Regex issue using ICU regex to find numbers not inside parentheses

北城余情 提交于 2019-12-07 18:59:34

问题


I'm trying to scan a given string for a number. The number cannot be after "v/v./vol/vol.", and cannot be inside parentheses. Here's what I have:

NSString *regex = @"(?i)(?<!v|vol|vol\\.|v\\.)\\d{1,4}(?![\\(]{0}.*\\))";
NSLog(@"Result: %@", [@"test test test 4334 test test" stringByMatching:regex]);
NSLog(@"Result: %@", [@"test test test(4334) test test" stringByMatching:regex]);
NSLog(@"Result: %@", [@"test test test(vol.4334) test test" stringByMatching:regex]);

Infuriatingly, this does not work. My regex can be separated into four parts:

(?i) - make regex case insensitive

(?<!v|vol|vol\\.|v\\.) - negative look-behind assertion for v/v./vol/vol.

\\d{1,4} - the number I'm looking for, 1-4 digits.

(?![\\(]{0}.*\\)) - negative look-ahead assertion: number cannot be preceding a ), unless there's a ( before it.

Maddeningly, if I take out the look-behind assertion, it works. What's the issue here? I'm using RegexKitLite, which uses the ICU regex syntax.


回答1:


Your negative lookbehind is positioned incorrectly. Lookbehind's do not modify the input position, your negative lookbehind should come after your \d{1,4} expression:

(?i)\\d{1,4}(?<!v|vol|vol\\.|v\\.)(?![\\(]{0}.*\\))

Alternatively, just use a negative lookahead to accomplish the same purpose:

(?i)(?!v|vol|vol\\.|v\\.)\\d{1,4}(?![\\(]{0}.*\\))



回答2:


Finally ended up with this regex:

(?i)\\d{1,4}(?<!v|vol|vol\\.|v\\.)(?![^\\(]*\\))

The negative look-behind needed to change. Passes all my tests. Thanks to Alex for identifying the positioning of my NLB being wrong.



来源:https://stackoverflow.com/questions/4250114/regex-issue-using-icu-regex-to-find-numbers-not-inside-parentheses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!