vb.net - Regex if match then replace -
i've looked around try find answer question, can't find i'm looking for. seems there should way decide if there match , replace, otherwise else without need repeat matching.
i'm trying decide if test string contains html document ends in
</body></html>
and inject text directly ahead of tags. of course there might combination of white-space/carriage-returns/line-feeds between 2 tags, i'm trying regex. however, test string might plain text , if regex match fails, append text end of string. , of course, i'm making more difficult is.
i don't have code show here since can't figure out if possible .net regex implementation, here psudo-code showing do:
dim teststring string = file contents dim reg new regex("(<\/body>\s*<\/html>)", regexoptions.ignorecase) dim rmatch match = reg.match(teststring) if rmatch.success rmatch.replace(newstring) else teststring &= alternatenewstring end if
of course need put end body , end html tags newstring close document, should no problem. part can't seem implement match replacement without need run regex again. seems calling match determine if match, calling replace making run twice. , again, might on thinking this, or prematurely optimizing. think?
if understand correctly, you're trying (which isn't good, see below):
dim teststring string = "your original string" dim newstr string = string.empty dim texttoinsert string = "your text 'inject'" dim reg new regex("<\/body>\s*<\/html>", regexoptions.ignorecase) newstr = reg.replace(teststring, texttoinsert & environment.newline & "</body></html>") if newstr = teststring newstr = teststring & environment.newline & texttoinsert end if
that work, compared matching twice, won't better in respect of performance.
so, better alternative let regex work (i.e. matching/replacing either closing tags or end of string). in case, change pattern this: \s*(<\/body>\s*<\/html>)|$
.
note:
|$
means "or end of string".- your original pattern put in capturing group
()
can access later when replacing.
using way, code following:
dim teststring string = "your original string" dim newstr string = string.empty dim texttoinsert string = "your text 'inject'" dim reg new regex("\s*(<\/body>\s*<\/html>)|$", regexoptions.ignorecase) newstr = reg.replace(teststring, environment.newline & texttoinsert & environment.newline & "$1", 1)
where:
$1
represents first group,</body>
,</html>
number whitespace characters in between.- the last argument in
replace
function maximum number of matched strings should replaced. set1
in order prevent inserting text before both closing tags and end of string.
hope helps :)
Comments
Post a Comment