Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSRegularExpression with arithmetic

I would like to change the font-size in an HTML string that I have to be half of its size.

E.g.

<div style="font-family:'Arial';font-size:43px;color:#ffffff;">

Will be

<div style="font-family:'Arial';font-size:21.5px;color:#ffffff;">

and

<div style="font-size:12px;">

Will be

<div style="font-size:6px;">

How can I do it with NSRegularExpression?

Please note that 12 and 6 and 43 and 21.5 are only examples. I need regex since it has to be a general solution for different font-size

like image 573
Dejell Avatar asked Dec 21 '22 11:12

Dejell


1 Answers

Use a real HTML parser to preserve your sanity. An XML parser for this is incredibly fragile. There are a dozen different perfectly valid variants of the HTML syntax that will break NSAddict's expression.

I suggest reading the top voted answer on this question as it applies equally as well to HTML as it does to XHTML or XML.

RegEx match open tags except XHTML self-contained tags

Note that the iOS / OS X system frameworks include HTML/XML parsing capabilities. Use those.

like image 139
bbum Avatar answered Jan 01 '23 18:01

bbum