Results 1 to 3 of 3

Thread: [RESOLVED] [2008] RegEx, finding and replacing a certain substring.

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Resolved [RESOLVED] [2008] RegEx, finding and replacing a certain substring.

    Hey there.
    I've got a string which could or could not contain the substring "/announce". I would like to replace this part of the string with "/scrape" if its found...so far nothing tricky. But heres the thing: I only want to replace it if the '/' in it is the last occurring in the string. Heres what I mean:

    Heres a scenario where I want the replace to happen:

    MyStringWithRandomText/announce?x=19
    would become:
    MyStringWithRandomText/scrape?x=19

    And heres another scenario where the '/' in announce is not the last '/' occuring in the string, so I dont want to replace it:

    MyStringWithRandomText/announce/foo?x=19

    I hope that made sense.
    Not sure if there is a way to handle this by only using RegEx
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] RegEx, finding and replacing a certain substring.

    it is possible. try this:

    vb Code:
    1. Dim testStr As String = "MyStringWithRandomText/announce/foo?x=19"
    2.  
    3. testStr = Regex.Replace(testStr, "/announce(?!.*/)", "/scrape")

  3. #3

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] RegEx, finding and replacing a certain substring.

    Impressive Paul, thanks alot!
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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