Search code examples
regexreplacematch

regex avoid replace number from (.*) replacement


I want to remove part of string using start and end words in string. At the same i want to keep number appear in this range.

String:

<FORMAT=T>8</FORMAT><FORMAT=ty>45</FORMAT>

Regex:

/(<FORMAT=T>).[^<FORMAT]*(<\/FORMAT>)/gm

output

<FORMAT=ty>45</FORMAT>

Expected output: I want keep number appear in between <FORMAT=T>...</FORMAT>

8<FORMAT=ty>45</FORMAT>


Solution

  • It looks like you want substitution (search/replace):

    • Match: <FORMAT=T>(\d+)</FORMAT> where the number between the tags is captured. \d may need to be [0-9] in some engines.
    • Substitute with \1 or $1 depending on your engine.

    Demo