Regular expression not giving the correct output

39 views Asked by At

I have a string like this

#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

From here I would like to extract these mp3 file names, so I put like this

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
}

But its not going inside of this block. Why is that. If my regular expression is wrong then what is the correct way I should write?

Thank you

1

There are 1 answers

0
Avinash Raj On

Use a lookahead to match the strings which are followed by .mp3

\w+(?=\.mp3)

nsregularexpression would be,

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

OR

Use your own regex and print the group index 1 to get the filename.