Search code examples
regexvisual-studio-codefind-replace

Find and Replace only portion of Regex Expression in IDE


I'm trying to fix malformed XML with Regex in VS Code.

Problem code is self-closing tags. On build of the project, these get nested as children. So I just need a simple regex pattern to match certain elements only (polygon, here), and ignore others.

Using Regex pattern:

(?:\.[0-9]+\"\s)\/\>

enter image description here

I want to search: tags containing a decimal point, number(s), and end double quote. I want to replace: everything after that.

Problem -- it replaces the entire part, instead of the chunk in the second expression ()

Example:

...
<stop offset="1" stop-color="#7ccb13" />
<stop offset="1" stop-color="#7ccb55" />
...
<polygon points="393.38 285.04 394.44 284.63 393.67 283.79 394.61 284.38 394.59 283.31 394.94 284.34 395.79 283.54 395.21 284.5 396.3 284.49 395.28 284.83 396.16 285.64 395.05 285.05 395.12 286.27 394.73 285.1 393.89 285.88 394.43 284.99 393.38 285.04" />
<polygon points="159.41 439.9 160.88 439.35 159.82 438.19 161.12 439.01 161.08 437.52 161.57 438.95 162.74 437.84 161.94 439.16 163.46 439.15 162.04 439.63 163.25 440.75 161.72 439.92 161.82 441.61 161.28 440 160.12 441.07 160.87 439.84 159.41 439.9" />
<polygon points="180.71 444.75 182.18 444.2 181.11 443.04 182.41 443.86 182.38 442.37 182.87 443.8 184.04 442.69 183.24 444.01 184.75 444 183.33 444.48 184.55 445.59 183.02 444.77 183.12 446.46 182.57 444.85 181.42 445.92 182.16 444.69 180.71 444.75" />
...

enter image description here

Desired output -- closing tags: enter image description here


Solution

  • I think (<polygon.+)(\/>) should work, and replace with $1></polygon>.

    example