Erhan Demirci Erhan Demirci - 1 year ago 105
iOS Question

Replace specific words in html string

I have dynamic string that currentString.
For example currentstring like :

<html><head><title></title><meta content="width=320.000000, initial-scale=0.47, maximum-scale=1.0, user-scalable=1" name="viewport"></head><body><table width="510" cellpadding="0" cellpadding="0"><tr><td valign="top"><p><a href=""><img src="" width="72" height="48" style="border: 1px #000000 solid;" hspace="2" align="left"></a>content...........</p> <p> </p> </td></tr></table></body></html>

I want to change table tag's width from 510 to 0. I tried following code but it's not working .

NSString *currentString = @"<html><...width > <table width="" .... > dynamic string";

// Regular expression to find "word characters" enclosed by {...}:
NSRegularExpression *regex;
regex = [NSRegularExpression regularExpressionWithPattern:@"\\table width=\"(\\w+)\\\""

NSMutableString *modifiedString = [currentString mutableCopy];
__block int offset = 0;
[regex enumerateMatchesInString:currentString
range:NSMakeRange(0, [currentString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
// range = location of the regex capture group "(\\w+)" in currentString:
NSRange range = [result rangeAtIndex:1];
// Adjust location for modifiedString:
range.location += offset;

// Get old word:
NSString *oldWord = [modifiedString substringWithRange:range];

// Compute new word:
// In your case, that would be
// NSString *newWord = [self replaceWord:oldWord];
NSString *newWord =@"0";

// Replace new word in modifiedString:
[modifiedString replaceCharactersInRange:range withString:newWord];
// Update offset:
offset += [newWord length] - [oldWord length];

NSLog(@"modified%@", modifiedString);

Answer Source

You got it almost right, only that @"\\table ... should be @"\\<table ... in the pattern:

regex = [NSRegularExpression regularExpressionWithPattern:@"\\<table width=\"(\\w+)\\\""

This would replace <table width="NNN" by <table width="0" for arbitrary NNN.

Note that generally it is not recommended to parse HTML with regular expressions. Using a dedicated HTML parser might be the better way to go.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download