Reputation: 1292
I'm trying to extract (80.4) from the following code:
<div id="row" style="width:80.4px">
What would the expression look like to extract that text? Thanks.
Upvotes: 1
Views: 573
Reputation: 16861
A regex is rather heavyweight for this particular situation. I would just do this:
NSString *originalString; //which will contain "<div id="row" style="width:80.4px">", however you want to get it there
NSString *afterColon = [[originalString componentsSeparatedByString:@":"] objectAtIndex:1];
float theValue = [afterColon floatValue];
Upvotes: 1
Reputation: 3250
Here are two possibilities but the answer will vary depending on the following factors:
1) what other content is in the text that you do NOT want to match against,
2) and what variations will you permit to match against (eg. adding spaces or new lines inside the test that you want to match, or swapping the order of the parts of the text to match)
This matches only the "width:80.4px" portion of your given string (allowing for extra white space):
width\s*:\s*(\d+.\d+)\s*px
And this matches the entire string that you gave (also allowing for extra white space):
<div\s+id\s*=\s*"row"\s+style\s*=\s*"width:\s*(\d+.\d+)\s*px">
So in these regexs the 80.4 will be captured in the $1 capture group.
Upvotes: 1