将正则表达式与字符串(文件名)匹配

时间:2021-08-25 19:01:50

I'm trying to differentiate between 2 files (in NSString format). As far as I know, this can be done by comparing and matching a regular expression. The format of the 2 jpg files which I have are:

我试图区分2个文件(NSString格式)。据我所知,这可以通过比较和匹配正则表达式来完成。我拥有的2个jpg文件的格式是:

butter.jpg

butter.jpg

butter-1.jpg

黄油1.JPG

My question is what regular expression can I write to match the 2 strings above? I've search and found an example expression, but I'm not sure how is it read and think it's wrong.

我的问题是我可以编写正则表达式来匹配上面的2个字符串吗?我已经搜索并找到了一个示例表达式,但我不确定它是如何阅读并认为它是错误的。

Here is my code:

这是我的代码:

NSString *exampleFileName = [NSString stringWithFormat:@"butter-1.jpg"];

NSString *regEx = @".*l{2,}.*";    

NSPredicate *regExTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regEx];

if ([regExTest evaluateWithObject:exampleFileName] == YES) {
    NSLog(@"Match!");
} else {
    NSLog(@"No match!");
}

EDIT:

编辑:

I tried using the following:

我尝试使用以下内容:

NSString *regEx = @"[a-z]+-[0-9]+.+jpg"; 

to try to match:

尝试匹配:

NSString *exampleFileName = [NSString stringWithFormat:@"abcdefg-112323.jpg"];

Tested with:

经测试:

abc-11.jpg (Match)

abc-11.jpg(匹配)

abcsdas-.jpg (No Match)

abcsdas-.jpg(不匹配)

abcdefg11. (No Match)

abcdefg11。 (不匹配)

abcdefg-3123.jpg (Match)

abcdefg-3123.jpg(匹配)

As of now it works, but I want to eliminate any chances that it might not, any inputs?

截至目前它是有效的,但我想消除它可能没有任何机会,任何输入?

2 个解决方案

#1


4  

NSString *regEx = @"[a-z]+-[0-9]+.+jpg"; 

will fail for butter.jpg, as it needs to have one - and at least on number.

butter.jpg会失败,因为它需要有一个 - 至少在数量上。

NSString *regEx = @"[a-z]+(-[0-9]+){0,1}.jpg"; 

and if you do

如果你这样做

NSString *regEx = @"([a-z])+(?:-([0-9])+){0,1}.jpg"; 

You can access the informations you probably would like to have later as capture groups.

您可以访问以后可能希望作为捕获组的信息。

(...) |Capturing parentheses. Range of input that matched the parenthesized subexpression is available after the match.

(...)|捕获括号。匹配括号子表达式的输入范围在匹配后可用。

and if you dont need capture groups

如果你不需要捕获组

NSString *regEx = @"(?:[a-z])+(?:-[0-9]+){0,1}.jpg"; 

(?:...)| Non-capturing parentheses. Groups the included pattern, but does not provide capturing of matching text. Somewhat more efficient than capturing parentheses.

(?:...)|非捕获括号。对包含的模式进行分组,但不提供匹配文本的捕获。比捕获括号更有效。

#2


2  

You can match an alphabetic character (in any language) using \p{L}. You can match a digit using \d. You need to escape the . because in a regular expression, . means “any character”.

您可以使用\ p {L}匹配字母字符(使用任何语言)。您可以使用\ d匹配数字。你需要逃避。因为在正则表达式中。意思是“任何角色”。

Parsing a regular expression is expensive, so you should only do it once.

解析正则表达式很昂贵,所以你应该只做一次。

BOOL stringMatchesMyPattern(NSString *string) {
    static dispatch_once_t once;
    static NSRegularExpression *re;
    dispatch_once(&once, ^{
        re = [NSRegularExpression regularExpressionWithPattern:
            @"^\\p{L}+-\\d+\\.jpg$" options:0 error:NULL];
    }

    return nil != [re firstMatchInString:string options:0
        range:NSMakeRange(0, string.length)];
}

#1


4  

NSString *regEx = @"[a-z]+-[0-9]+.+jpg"; 

will fail for butter.jpg, as it needs to have one - and at least on number.

butter.jpg会失败,因为它需要有一个 - 至少在数量上。

NSString *regEx = @"[a-z]+(-[0-9]+){0,1}.jpg"; 

and if you do

如果你这样做

NSString *regEx = @"([a-z])+(?:-([0-9])+){0,1}.jpg"; 

You can access the informations you probably would like to have later as capture groups.

您可以访问以后可能希望作为捕获组的信息。

(...) |Capturing parentheses. Range of input that matched the parenthesized subexpression is available after the match.

(...)|捕获括号。匹配括号子表达式的输入范围在匹配后可用。

and if you dont need capture groups

如果你不需要捕获组

NSString *regEx = @"(?:[a-z])+(?:-[0-9]+){0,1}.jpg"; 

(?:...)| Non-capturing parentheses. Groups the included pattern, but does not provide capturing of matching text. Somewhat more efficient than capturing parentheses.

(?:...)|非捕获括号。对包含的模式进行分组,但不提供匹配文本的捕获。比捕获括号更有效。

#2


2  

You can match an alphabetic character (in any language) using \p{L}. You can match a digit using \d. You need to escape the . because in a regular expression, . means “any character”.

您可以使用\ p {L}匹配字母字符(使用任何语言)。您可以使用\ d匹配数字。你需要逃避。因为在正则表达式中。意思是“任何角色”。

Parsing a regular expression is expensive, so you should only do it once.

解析正则表达式很昂贵,所以你应该只做一次。

BOOL stringMatchesMyPattern(NSString *string) {
    static dispatch_once_t once;
    static NSRegularExpression *re;
    dispatch_once(&once, ^{
        re = [NSRegularExpression regularExpressionWithPattern:
            @"^\\p{L}+-\\d+\\.jpg$" options:0 error:NULL];
    }

    return nil != [re firstMatchInString:string options:0
        range:NSMakeRange(0, string.length)];
}