Results 1 to 7 of 7

Thread: Regex.Replace match with stuff + match?

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    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

  2. #2

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    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

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Regex.Replace match with stuff + match?

    Before you get regex gung ho, try something like this:
    VB Code:
    1. Dim yourstring As String = "${Wild Bill}"
    2.         yourstring = yourstring.Replace("${", "<b>${")
    3.         yourstring = yourstring.Replace("}", "}<\b>")

  4. #4

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Regex.Replace match with stuff + match?

    Quote Originally Posted by wild_bill
    Before you get regex gung ho, try something like this:
    VB Code:
    1. Dim yourstring As String = "${Wild Bill}"
    2.         yourstring = yourstring.Replace("${", "<b>${")
    3.         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

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Regex.Replace match with stuff + match?

    It will definately be tricky, do you have a sample of the text you wish to parse?

  6. #6
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Bath, England
    Posts
    411

    Re: Regex.Replace match with stuff + match?

    Something like this ought to do it:
    VB Code:
    1. Dim inputString As String = "This ${is a test.}  It hopefully ${does the job}"
    2.         Dim regexString As String = "(\$\{).+(\})"
    3.         Dim regexMatch As String = ""
    4.  
    5.         Do
    6.             regexMatch = Regex.Match(inputString, regexString).Value
    7.             If Not inputString.Equals("") Then inputString = inputString.SubString(2, inputString.Length - 3)
    8.         Loop While Not regexMatch.Equals("")
    9.  
    10.         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"

  7. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Regex.Replace match with stuff + match?

    VB Code:
    1. Dim inputString As String = "This ${is a {test}.}  Random header ${. It hopefully ${does the job}"
    2.         Dim Headers() As String = Regex.Split(inputString, "\$\{")
    3.         Dim outputstring As String
    4.         For Each mt As String In Headers
    5.             Dim i As Int32 = mt.LastIndexOf("}"c)
    6.             If i > 0 Then
    7.                 outputstring = outputstring.Insert(outputstring.Length - 2, "<b>")
    8.                 outputstring &= mt.Insert(i + 1, "</b>")
    9.             Else
    10.                 outputstring &= mt
    11.             End If
    12.             outputstring &= "${"
    13.         Next
    14.  
    15.         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
  •  



Click Here to Expand Forum to Full Width