0

I have this string:

<x>&nbsp;<span class='var'>or<tg>test<pk>testing</pk></tg><tx>or</tx><tg>teste<pk>testando</pk></tg></span></x>

I'm using this pattern:

<tg>(.*)<pk>(.*)</pk></tg>

But it's replacing from the first <tg> to the last one, ignoring the </tg> in the middle.

My replacement string:

<a href='def.aspx?&word=$2'>$1</a>
Cornwell
  • 3,304
  • 7
  • 51
  • 84

1 Answers1

2

.* is greedy so it's continuing even after finding matches. You can tweak your regular expression slightly to change this behavior:

<tg>([^<]*)<pk>([^<]*)</pk></tg>

(that is, allow anything except for an opening < inside of <tg> and <pk> tags using a negated character class)

or:

<tg>(.*?)<pk>(.*?)</pk></tg>

(use the lazy quantifier with * to make the engine match as few occurrences as possible)

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307