regex avoid replace number from (.*) replacement

42 views Asked by At

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>

2

There are 2 answers

0
Ted Lyngmo On BEST ANSWER

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

0
Dale On

It'd be helpful to know what regex engine you're running as they're all a little different, but I'll take a stab and hope the syntax is correct. You should update your question to provide the particular engine you're running.

/<FORMAT=T>(\d+)<\/FORMAT>(<FORMAT=ty>\d+<\/FORMAT>)/\1\2/gm

you might want to review the global and multiline modifiers too, I suspect they aren't necessary, but can't say for sure without more of the context.