I'm looking in a string for anything that starts with "${" and ends with "}", and then replace it with "<b>whatever the match was</b>".
Is this possible? Whoever's answer I like best gets Marty's job for a week.
:afrog:
Printable View
I'm looking in a string for anything that starts with "${" and ends with "}", and then replace it with "<b>whatever the match was</b>".
Is this possible? Whoever's answer I like best gets Marty's job for a week.
:afrog:
Nobody can even tell me to shut the hell up? I've lost all faith in you people.
:(
Before you get regex gung ho, try something like this:
VB Code:
Dim yourstring As String = "${Wild Bill}" yourstring = yourstring.Replace("${", "<b>${") yourstring = yourstring.Replace("}", "}<\b>")
Quote:
Originally Posted by wild_bill
Well, what if there is a "${" without a matching "}", or vice versa, which is a distinct possibility with what I'm doing...
:cool:
It will definately be tricky, do you have a sample of the text you wish to parse?
Something like this ought to do it:
I think there is a way to get all the matches in one go using the "Matches" class, but this works just as well; you'd have to loop through all the matches anyway.VB Code:
Dim inputString As String = "This ${is a test.} It hopefully ${does the job}" Dim regexString As String = "(\$\{).+(\})" Dim regexMatch As String = "" Do regexMatch = Regex.Match(inputString, regexString).Value If Not inputString.Equals("") Then inputString = inputString.SubString(2, inputString.Length - 3) Loop While Not regexMatch.Equals("") MessageBox.Show(inputString)
VB Code:
Dim inputString As String = "This ${is a {test}.} Random header ${. It hopefully ${does the job}" Dim Headers() As String = Regex.Split(inputString, "\$\{") Dim outputstring As String For Each mt As String In Headers Dim i As Int32 = mt.LastIndexOf("}"c) If i > 0 Then outputstring = outputstring.Insert(outputstring.Length - 2, "<b>") outputstring &= mt.Insert(i + 1, "</b>") Else outputstring &= mt End If outputstring &= "${" Next MessageBox.Show(outputstring.Substring(0, outputstring.Length - 2))