4

I want to insert &nbsp between empty paragraph tags.

Input:

1. <div><p></p></div>
2. <div><p style="margin: 0 0 0 0"></p></div>

Expected output:

1. <div><p>&nbsp;</p></div>
2. <div><p style="margin: 0 0 0 0">&nbsp;</p></div>

I tried to use look behind and forward but apparently I can't use quantifier here: (?<=< p[^>]*>)(?=<\/p>)

Any ideas? My available tools are regex and c#

Pruthvi Raj
  • 3,016
  • 2
  • 22
  • 36
SZT
  • 1,771
  • 4
  • 26
  • 53

3 Answers3

6

You may be overthinking. I think all you need is to simply replace ></p> with >&nbsp;</p>, isn't it?

yourString = yourString.Replace("></p>", ">&nbsp;</p>");

EDIT

If you must use RegEx, then you do like this:

yourString = Regex.Replace(yourString, "(<p[^>]*>)(</p>)", "$1&nbsp;$2", RegexOptions.IgnoreCase);
Pradeep Kumar
  • 6,836
  • 4
  • 21
  • 47
  • with your first suggestion

    will also get replaced, i am looking into your second option, what does the $1 $2 do?
    – SZT Sep 02 '15 at 21:25
0

Get all paragraphs tag and check their inner text if empty.

If its empty do something like getElementsByTagName('p')[0].innerHTML= '&nbsp';

raym0nd
  • 3,172
  • 7
  • 36
  • 73
  • i can't use javascript for this one. this is basically a xaml file converted to html, and I am trying to get rid of some noise – SZT Sep 02 '15 at 20:08
  • Oops, well then [this](http://stackoverflow.com/a/32362354/810610) answer should be it. – raym0nd Sep 02 '15 at 20:39
0
(?<=>)(\s*)(?=<\/p>)
Cody Bouche
  • 945
  • 5
  • 10