1
 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.

Helios21cz
  • 27
  • 5
  • You mean `Regex.Replace(text, @"(?<=\bdisposition=""[^""]*)>", "R")`? – Wiktor Stribiżew Jan 19 '22 at 12:11
  • @WiktorStribiżew That works, thank you very much. So do I understand correctly that in Regex whitespaces makes word boundary, so moving to start of word makes my request possible? Rn I don't even know what ```[^""]``` does... but it works and I don't really plan to study Regex anytime soon. Thanks for working solution. – Helios21cz Jan 19 '22 at 13:17

1 Answers1

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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563