0

I have this text:

<ITEM>PART 23 <span class='sub'>m1</span>, 
<ITEM>PART 49b
;

I want the output to be:

<b>PART 23 </b><span class='sub'>m1</span>, <b>PART 49b</b>;

I'm using this pattern: <ITEM>(.*?)(?=<|\n|$) Which results in this:

<b>PART 23 </b><span class='sub'>m1</span>, 
<b>PART 49b</b>
;

(notice the semi-colon)

If I change the pattern to this: <ITEM>(.*?)(<|\n|$) The output will be this:

<b>PART 23 </b>span class='sub'>m1</span>, 
<b>PART 49b</b>;

(notice the missing < in the span>

What am I missing?

Cornwell
  • 3,304
  • 7
  • 51
  • 84
  • 1
    In before someone links to the bobince answer. –  Aug 18 '17 at 17:41
  • Let me do the honors. https://stackoverflow.com/a/1732454/302248 – Sach Aug 18 '17 at 17:43
  • 1
    Expected. Next time I'll just remove the tags and put other characters since apparently the sight of one html tag in a regex question brings that link. Even if not remotely related to the question. – Cornwell Aug 18 '17 at 17:48
  • 1
    Try `(?:\r?\n)*(.*?)(?:\s*(;))?([<\r\n]|$)` and replace with `$1$2$3`, see [this demo](http://regexstorm.net/tester?p=%28%3f%3a%5cr%3f%5cn%29*%3cITEM%3e%28.*%3f%29%28%3f%3a%5cs*%28%3b%29%29%3f%28%5b%3c%5cr%5cn%5d%7c%24%29&i=%3cITEM%3ePART+23+%3cspan+class%3d%27sub%27%3em1%3c%2fspan%3e%2c+%0d%0a%3cITEM%3ePART+49b%0d%0a%3b&r=%3cb%3e%241%3c%2fb%3e%242%243). – Wiktor Stribiżew Aug 18 '17 at 18:04
  • What is your replacement? – NetMage Aug 18 '17 at 20:47

1 Answers1

1

This seems to work with your limited sample:

var match = @"<ITEM>([^<\n]+)\n?";
var replace = @"<b>$1</b>";

var ans = Regex.Replace(src, match, replace);
NetMage
  • 26,163
  • 3
  • 34
  • 55