Home > Net >  Extract everything after a specific string using a variable in regex
Extract everything after a specific string using a variable in regex

Time:07-19

i am trying to match everything in a RichTextBox after a string variable using Regex.

The normal codei have to match after something looks like this:

Dim answ As Regex = New Regex("(?<=MATCH_AFTER_THIS). ")
    Dim match As Match = answ.Match(Richtextbox1.Text)

i now need to replace "Match after this" with a string variable, looks like this at the moment:

New Regex(stringvariable & ". ") 

However this matches INCLUDING what the variable contains, i guess i am missing the "?<=", but i just cant work out where to put it correctly.

Can someone help?

TiA

CodePudding user response:

You need to keep the lookbehind construct by adding the variable in between (?<= and ).

If stringvariable is a regex pattern, all you need is

New Regex("(?<=" & stringvariable & "). ")

If your string variable is a fixed string, not a regex, you need to remember to escape it:

New Regex("(?<=" & Regex.Escape(stringvariable) & "). ")
  • Related