regexregex-greedy

Regex Manupulation


My input is

String input = "I am doing <span id='icms'>test</span>on <span id='icms'>regex</span> exp."

Now My output is coming like

"regex exp"

But my output is needed like this

I am doing test on regex exp

So need help on the regex and matcher.


Solution

  • You need two replacements. First replace <span[^>]*id=['"]icms['"][^>]*>([^<]*)</span> by ' $1 ' (without the quotes; they just show you that you have to pre- and append a space) and then replace ' {2,}' (without the quotes again) by a single space.

    Quick PowerShell test:

    PS> "I am doing <span id='icms'>test</span>on <span id='icms'>regex</span> exp." -replace '<span[^>]*id=[''"]icms[''"][^>]*>([^<]*)</span>',' $1 ' -replace ' {2,}',' '
    I am doing test on regex exp.