3

In the example below, how to preserve part of a pattern? The pattern search must include closing tag </h3> as spans elsewhere must not be targeted.

Example: remove only </span> from </span> some text </h3>:

regEx.Pattern = "</span>[^>]*</h3>"
hr2 = regEx.Replace(hr2,"[^>]*</h3>")  '<- does not work
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Paul
  • 117
  • 2
  • 9

1 Answers1

3

You need to set a capturing group and use a back reference in the replacement pattern.

regEx.Pattern = "</span>([^>]*)</h3>"
hr2 = regEx.Replace(hr2,"$1</h3>")

Or

regEx.Pattern = "</span>([^>]*</h3>)"
hr2 = regEx.Replace(hr2,"$1")
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563