使用regex捕获的NSPredicate总是得到0结果。

时间:2022-08-23 11:33:45

Hi to all overflowers,

嗨,overflowers

I'm scratching my head around putting a regular expression inside an NSPredicate.

我正绞尽脑汁地在NSPredicate中加入一个正则表达式。

I would like to move all our thumbnails from Documents directory into Caches directory and catch em'all I've created this regex: _thumb(@[2-3]x)?\.jpg.

我想将所有的缩略图从Documents目录移动到cache目录中,并捕获我创建的regex: _thumb(@[2-3]x)?\.jpg。

Here on regex101.com you can see the above regex working with this test data:

在regex101.com上,您可以看到上面的regex正在处理这个测试数据:

grwior_thumb.jpg          <- match
grwior.jpg
vuoetrjrt_thumb@2x.jpg    <- match
vuoetrjrt.jpg
hafiruwhf_thumb.jpg       <- match
hafiruwhf_thumb@2x.jpg    <- match
hafiruwhf_thumb@3x.jpg    <- match
hafiruwhf.jpg

But when I put it in the code it's not matching anything:

但是当我把它放入代码时它没有匹配任何东西:

NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];

// Find and move thumbs to the caches folder
NSArray<NSString *> *mediaFilesArray = [fileManager contentsOfDirectoryAtPath:documentsPath error:&error];

NSString *regex = @"_thumb(@[2-3]x)?\\.jpg";
NSPredicate *thumbPredicate = [NSPredicate predicateWithFormat: @"SELF ENDSWITH %@", regex];

NSArray<NSString *> *thumbFileArray = [mediaFilesArray filteredArrayUsingPredicate:thumbPredicate];

thumbFileArray has always 0 elements...

thumbFileArray有0个元素…

What am I doing wrong?

我做错了什么?

2 个解决方案

#1


2  

Use MATCHES rather than ENDSWITH, as ENDSWITH does not treat the expression as a regular expression, but make sure you match all the chars from the start of the string, too, as MATCHES requires a full string match, so you need to somehow match the chars before the _.

使用MATCHES而不是ENDSWITH,因为ENDSWITH并不将表达式视为正则表达式,而是确保从字符串的开始就匹配所有的chars,因为match需要一个完整的字符串匹配,所以您需要在_之前以某种方式匹配这些chars。

Use

使用

NSString *regex = @".*_thumb(@[23]x)?\\.jpg";

And then

然后

[NSPredicate predicateWithFormat: @"SELF MATCHES %@", regex]

The .* will match any 0+ chars other than line break chars, as many as possible.

*将匹配任意的0+ chars,而不是行中断字符,尽可能多。

Note that if you just want to match either 2 or 3, you might as well write [23], no need for a - range operator here.

注意,如果你只想匹配2或3,你可以写[23],这里不需要一个- range操作符。

You may also replace (@[23]x)? with (?:@[23]x)?, i.e. change the capturing group to a non-capturing, since you do not seem to need the submatch to be accessible later. If you do, keep the optional capturing group.

你也可以替换(@[23]x)?与(?:@[23]x)?,即将捕获组更改为非捕获组,因为您似乎不需要稍后才能访问子匹配。如果需要,保留可选捕获组。

#2


1  

The problem is with ENDSWITH.

问题出在ENDSWITH身上。

ENDSWITH
The left-hand expression ends with the right-hand expression.
MATCHES
The left hand expression equals the right hand expression using a regex-style comparison according to ICU v3

What you need is

你需要的是

NSString *regex = @".+_thumb(@[2-3]x)?\\.jpg";
NSPredicate *thumbPredicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", regex];

#1


2  

Use MATCHES rather than ENDSWITH, as ENDSWITH does not treat the expression as a regular expression, but make sure you match all the chars from the start of the string, too, as MATCHES requires a full string match, so you need to somehow match the chars before the _.

使用MATCHES而不是ENDSWITH,因为ENDSWITH并不将表达式视为正则表达式,而是确保从字符串的开始就匹配所有的chars,因为match需要一个完整的字符串匹配,所以您需要在_之前以某种方式匹配这些chars。

Use

使用

NSString *regex = @".*_thumb(@[23]x)?\\.jpg";

And then

然后

[NSPredicate predicateWithFormat: @"SELF MATCHES %@", regex]

The .* will match any 0+ chars other than line break chars, as many as possible.

*将匹配任意的0+ chars,而不是行中断字符,尽可能多。

Note that if you just want to match either 2 or 3, you might as well write [23], no need for a - range operator here.

注意,如果你只想匹配2或3,你可以写[23],这里不需要一个- range操作符。

You may also replace (@[23]x)? with (?:@[23]x)?, i.e. change the capturing group to a non-capturing, since you do not seem to need the submatch to be accessible later. If you do, keep the optional capturing group.

你也可以替换(@[23]x)?与(?:@[23]x)?,即将捕获组更改为非捕获组,因为您似乎不需要稍后才能访问子匹配。如果需要,保留可选捕获组。

#2


1  

The problem is with ENDSWITH.

问题出在ENDSWITH身上。

ENDSWITH
The left-hand expression ends with the right-hand expression.
MATCHES
The left hand expression equals the right hand expression using a regex-style comparison according to ICU v3

What you need is

你需要的是

NSString *regex = @".+_thumb(@[2-3]x)?\\.jpg";
NSPredicate *thumbPredicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", regex];