[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:eek:
Re: [2008] RegEx, finding and replacing a certain substring.
it is possible. try this:
vb Code:
Dim testStr As String = "MyStringWithRandomText/announce/foo?x=19"
testStr = Regex.Replace(testStr, "/announce(?!.*/)", "/scrape")
Re: [2008] RegEx, finding and replacing a certain substring.
Impressive Paul, thanks alot!