.net - Prepend a URL segment to a relative / relative to server URL -
i have requirement prepend url segment relative, or relative-to-server urls within html document (eg href or src attributes) on server-side application
i'm working in .net environment, , unfortunately there no base class libraries loading html string dom , manipulating (i not have luxury of being able introduce third-party library @ point), seems candidate regex replace, i'm little weak on
i need regex can handle these 2 cases:
relative server url:
href="/controller/action" -> href="/mypathsegment/controller/action"
relative url:
href="image/logo.gif" -> href="/mypathsegments/image/logo.gif"
don't worry mypathsegment
..i have logic elsewhere can figure out levels of path segments relative urls, want focus on regex problem
i figure need regex can match on src="..."
or href="/..."
pattern , insert string @ beginning after first opening double quote not experienced enough regexs figure out.
you want find url, assert url not contain /mypathsegment/, , replace same property (href or src), , same end-of-url, in middle, put path segment:
(?<tag>(?:href|src)=")(?(?=/mypathsegment/)(?!)|/?(?<url>[^"]*"))
replace with:
${tag}/mypathsegment/${url}
in vb.net:
dim strpathprefix string = "/mypathsegment/" dim strtest string = regex.replace("<a href=""/controller/action"" property=""something"">hello world</a>", "(?<tag>(?:href|src)="")(?(?=" & strpathprefix & ")(?!))/?(?<url>[^""]*"")", "${tag}/mypathsegment/${url}") strtest = <a href="/mypathsegment/controller/action" property="something">hello world</a>
note in vb, had double quotes.
with use, whether or not have slash irrelevant. you'd have make sure strpathprepend ends "/"
test on things , let me know if hit bumps might require tweaking.
(?<tag>...
the data following should captured capture group named "tag".
(?:href|src)
match either "href" or "src". "?:" means not capture results particular sub-expression, though results capture parent sub-expression "tag"
=")
capture equal sign , quote symbol literally, , close capture group "tag"
(?(...)...|...)
example conditional statement. if in small sub-expression, left-side of alternation "|", else on right-side of alternation.
(?=...)
example lookaround. in case, lookaround "look-ahead". means: check if text following current position equals something, don't advance cursor.
(?(?=/mypathsegment/)...
so, put our 2 examples together, , conditional says... if, after collecting href=", find /mypathsegment/, then...
(?!)
this lookaround. negative lookahead. there nothing in it. (?!...). since negative lookahead , it's empty, automatically fails regex statement. want fail if sees path segment in there.
|/?
else, if /mypathsegment/ not found... optionally find slash, isn't in capture group.
(?<url>...
also in else statement... after optional slash, build our second capture group named "url"
[^"]*"
capture not quote... multiple times (optional, change * + if want require something), after catching not quote... capture quote "url" capture group also.
))
first closes url capture group, closes conditional group.
Comments
Post a Comment