Regex look behind until whitespace

 string corrected = Regex.Replace(input, @">(?<=disposition)", "R");

Is it possible to replace ">" only in cases that it is preceded by "disposition" without any whitespaces between it?

I'm asking because I have pseudo XML with attributes like disposition="4<^12^13>>^^<^14,5<^20" in many elements. I load it as one big string, do various fixes and only then I parse it to XML.

I can't think of many sollutions... In case Regex can't do what I'm asking, I can think only of separating that big string by whitespaces and fix every attribute individually then, but I'm afrad that will create a lot of load.


Solution 1:

You can use

Regex.Replace(text, @"(?<=\bdisposition=""[^""]*)>", "R")

Here, the regex is (?<=\bdisposition="[^"]*)>, the " is doubled only because the string literal is the verbatim string literal here.

Details:

  • (?<=\bdisposition="[^"]*) - a positive lookbehind ((?<=...)) that requires, immediately to the left of the current location):
    • \b - word boundary
    • disposition=" - a literal text
    • [^"]* - zero or more chars other than " char
  • > - a > char.