c# - Transform a String Using Regex -
i have html content need modify using c#. conceptually simple i'm not sure how efficiently. content contains several occurrences of delimited numbers followed empty anchor tag. need take delimited number , insert javascript function call in anchor tag. e.g.
the source string contain this:
%%1%%<a href="#"></a> <p>a bunch of html markup</p> %%2%%<a href="#"></a> <p>some more html markup</p> i need transform this:
<a href="#" onclick="dosomething('1')></a> <p>a bunch of html markup</p> <a href="#" onclick="dosomething('2')></a> <p>some more html markup</p> there no limit number of %%\d+%% occurrences. took crack @ writing regular expression in hopes use replace method, i'm not sure if can work multiple instances of each group. here's had:
%%(?<linkid>\d+)%%(?<linkstart><a[\s\s]*?)(?:(?<linkend>>[\s\s]*?)(?=%%\d+|$)) // %%(?<linkid>\d+)%% match number surrounded %% , put number in group named linkid // (?<linkstart><a[\s\s]*?) match <a followed characters until next match (non greedy), in group named linkstart // (?: logical grouping not captured // (?<linkend>>[\s\s]*?) match > followed characters until next match, in group named linkend // (?=%%\d+%%|$) former linkend group followed instance of delimited number or end of string. (i don't think working intended.) maybe combination of couple regex operations , string.format used. i'm not great @ regular expressions, please dumb down answers me bit.
using regex parse html has been covered extensively on so. the consensus should not done.
if need parse html recommend using html agility pack. allows use similar xpath identify html want work on.
Comments
Post a Comment