Reputation: 1963
For example the regular
expression is :
A(B)C
A,B,C all represent some string.I want all the string matches A(B)C replacing by B.
If the NSString
is AABCAABCBBABC:
The answear will be ABABBBB.How to do that? Thank you.
I give a more specific example:
<script\stype="text/javascript"[\s\S]*?(http://[\s\S]*?)'[\s\S]*?</script>
The answer is some script mathes and http://
url matches .
I want to use each http://
url matches to replace each script matches. Did I explain it clearly?
Upvotes: 0
Views: 1155
Reputation: 38259
One solution can be using stringByReplacingMatchesInString
:
NSString *strText = @"AABCAABCBBABC";
NSError *error = nil;
NSRegularExpression *regexExpression = [NSRegularExpression regularExpressionWithPattern:@"ABC" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *strModifiedText = [regexExpression stringByReplacingMatchesInString:strText options:0 range:NSMakeRange(0, [strText length]) withTemplate:@"B"];
NSLog(@"%@", strModifiedText);
Another solution can be using stringByReplacingOccurrencesOfString
:
strText = [strText stringByReplacingOccurrencesOfString:@"ABC" withString:@"B"];
Upvotes: 2