我認爲這可能是你的情況的解決方案,我用strong
HTML屬性作爲獨特的東西覆蓋每個部分,然後檢查匹配的字符串(部分)和理想的文本附加到每個部分(有我你知道HTML字符串我可以寫更多確切的代碼):
// sample HTML String
NSString *htmlString = @"<strong>Section (A)</strong><br>...<br><strong>Section (B)</strong><br>...<br><strong>Section (C)</strong><br>...<br>";
NSString *regexString = @"<strong>(.*?)</strong>"; // or whatever makes sense for your scenario
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:regexString
options:NSRegularExpressionCaseInsensitive
error:nil];
NSMutableString* mutableString = [htmlString mutableCopy];
NSInteger offset = 0; // keeps track of range changes in the string
// due to replacements.
for (NSTextCheckingResult* result in [regex matchesInString:htmlString
options:0
range:NSMakeRange(0, [htmlString length])]) {
NSRange resultRange = [result range];
resultRange.location += offset; // resultRange.location is updated
// based on the offset updated below
// implement your own replace functionality using
// replacementStringForResult:inString:offset:template:
// note that in the template $0 is replaced by the match
NSString* match = [regex replacementStringForResult:result
inString:mutableString
offset:offset
template:@"$0"];
NSLog(@"match %@", match);
NSString* append;
if ([match containsString:@"Section (A)"]) {
append = @"AppendForSectionA";
} else if ([match containsString:@"Section (B)"]) {
append = @"AppendForSectionB";
} else if ([match containsString:@"Section (C)"]) {
append = @"AppendForSectionC";
} else {
append = @"";
}
NSString* replacement = [match stringByAppendingString:append];
// make the replacement
[mutableString replaceCharactersInRange:resultRange withString:replacement];
// update the offset based on the replacement
offset += ([replacement length] - resultRange.length);
}
NSLog(@"resulting string: %@", mutableString);
其結果將是:陳前
<strong>Section (A)</strong>AppendForSectionA<br>...<br><strong>Section (B)</strong>AppendForSectionB<br>...<br><strong>Section (C)</strong>AppendForSectionC<br>...<br>
屏幕截圖GE:
難道這些標題具有相同的文字一如既往:更改後
屏幕截圖?他們只是粗體嗎? –
是的標題有相同的文字 – Hassy
你想改變裏面的字符串** Flexion:角度:0-5,質量:Crepitus **或角度,質量值 –