【问题标题】:Regular expression not giving the correct output正则表达式没有给出正确的输出
【发布时间】:2014-11-28 07:36:59
【问题描述】:

我有一个这样的字符串

#EXT-X-MEDIA-SEQUENCE:4138
#EXTINF:10.031,
media_w2031009843_4138.mp3
#EXTINF:10.031,
media_w2031009843_4139.mp3
#EXTINF:10.031,
media_w2031009843_4140.mp3

从这里我想提取这些mp3文件名,所以我这样放

NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:@"(\\w+).mp3" options:NSRegularExpressionCaseInsensitive error:&error];


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

        //some code is there
}

但它不会进入这个区块。这是为什么。如果我的正则表达式是错误的,那我应该怎么写?

谢谢

【问题讨论】:

标签: ios regex nsstring objective-c-blocks nsregularexpression


【解决方案1】:

使用前瞻匹配.mp3后面的字符串

\w+(?=\.mp3)

nsregularexpression 是,

\\w+(?=\\.mp3)

使用您自己的正则表达式并打印组索引 1 以获取文件名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 2015-07-17
    相关资源
    最近更新 更多