|
-
Dec 9th, 2005, 12:42 PM
#1
Regex.Replace match with stuff + match?
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.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 9th, 2005, 06:16 PM
#2
Re: Regex.Replace match with stuff + match?
Nobody can even tell me to shut the hell up? I've lost all faith in you people.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 9th, 2005, 06:38 PM
#3
Re: Regex.Replace match with stuff + match?
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>")
-
Dec 9th, 2005, 06:58 PM
#4
Re: Regex.Replace match with stuff + match?
 Originally Posted by wild_bill
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>")
Well, what if there is a "${" without a matching "}", or vice versa, which is a distinct possibility with what I'm doing...
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 11th, 2005, 12:48 PM
#5
Re: Regex.Replace match with stuff + match?
It will definately be tricky, do you have a sample of the text you wish to parse?
-
Dec 11th, 2005, 01:32 PM
#6
Hyperactive Member
Re: Regex.Replace match with stuff + match?
Something like this ought to do it:
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)
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.
Last edited by Parallax; Dec 12th, 2005 at 05:12 PM.
"Make it idiot-proof and someone will make a better idiot"
-
Dec 12th, 2005, 11:54 AM
#7
Re: Regex.Replace match with stuff + match?
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))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|