【问题标题】:How to split this string and get the number separately in ios regular expression如何在ios正则表达式中拆分此字符串并分别获取数字
【发布时间】:2015-02-07 02:27:46
【问题描述】:

我有一个NSStringlike this "media_w940996738_476.mp3" 我想单独获取这个 "476" 号码。如何使用正则表达式从这个 NSString 中获取它。

【问题讨论】:

  • 所有字符串的名称都与“media_w123456789_123.mp3”相似吗???
  • 是的,只有数字会改变

标签: ios regex nsstring nsregularexpression


【解决方案1】:

如果您总是想找出文件扩展名前用下划线分隔的最后一个值,请使用以下代码:

NSString *mediaName = [[fileName componentsSeparatedByString:@"."] firstObject];
int requiredNumber = [[[mediaName componentsSeparatedByString:@"_"] lastObject] intValue];

【讨论】:

  • 正确答案,但正则表达式在哪里?
  • 最好的代码总是简单而高效。如果可以用更简单的方式完成,为什么要显式使用正则表达式。
【解决方案2】:
Here is your regex. for this

NSString *yourString = @"media_w940996738_476.mp3";
NSError *error = NULL;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"([0-9]{3})([.]{1})" options:NSRegularExpressionCaseInsensitive error:&error];


[regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){

    // detect
    NSString *insideString = [yourString substringWithRange:[match rangeAtIndex:1]];

    //print
    NSLog(@"%@",insideString);

}];

【讨论】:

    【解决方案3】:

    您可以使用正则表达式:

    NSString *string = @"media_w940996738_476.mp3";
    
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"_([:digit:]+)\\." options:NSRegularExpressionCaseInsensitive error:nil];
    
        [regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
            // detect
            NSString *insideString = [string substringWithRange:[result rangeAtIndex:1]];
            //print
            NSLog(@"%@",insideString);
        }];
    

    【讨论】:

      【解决方案4】:

      如果您绝对不需要正则表达式,您可以简单地使用以下内容:

      NSInteger value = [[[fileName componentsSeparatedByString:@"_"] lastObject] integerValue];
      

      调用componentsSeparatedByString:@"_" 将返回一个数组,而lastObject 将是476.mp3

      获取integerValue 应该返回476

      【讨论】:

        【解决方案5】:

        使用正则表达式,您可以搜索 一个或多个数字 (\d)+ 以 .mp3 结尾 (\.mp3)$:

        NSString *filename = @"media_w940996738_476.mp3";
        
        NSError *error = NULL;
        
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d+)(\\.mp3)$" options:NSRegularExpressionCaseInsensitive error:&error];
        
        NSRange textRange = NSMakeRange(0, filename.length);
        NSTextCheckingResult *match = [[regex matchesInString:filename options:0 range:textRange] firstObject];
        NSString *matchedString = [filename substringWithRange:[match rangeAtIndex:1]];
        
        NSLog(@"%@", matchedString);
        

        如果您想匹配具有不同扩展名的文件名,您可以通过列出它们来更改正则表达式模式:

        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d+)(\\.(mp3|m4a|m4b|aa))$" options:NSRegularExpressionCaseInsensitive error:&error];
        

        要匹配任何带有 2 或 3 个字符扩展名的文件名:

        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d+)(\\.\\b\\w{2,3})$" options:NSRegularExpressionCaseInsensitive error:&error];
        

        【讨论】:

          猜你喜欢
          • 2023-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多