1

In the HTML page, I will need to match all innerHTML one by one.

I make a REGEX wich permit to match all tag except innerHTML (include BR tag) but I can not do the opposite...

([<][^br][^<]*[>])

You can see an example on this URL : https://regex101.com/r/h9tKHj/1


On this DOM :

<li class="product-faq-item">
          <p class="product-faq-title">{{XXXXXXXXXXXX1}}</p>
          <div class="product-faq-container">
            <p class="product-faq-text">{{XXXXXXXXXXXX2}}<br>
              {{XXXXXXXXXXXX3}}
            </p>
          </div>
        </li>

        <li class="product-faq-item">
          <p class="product-faq-title">{{XXXXXXXXXXXX4}}</p>
          <div class="product-faq-container">
            <p class="product-faq-text">{{XXXXXXXXXXXX5}}</p>
          </div>
        </li>

My goal is to recover this :

Match 1 : {{XXXXXXXXXXXX1}}
Match 2 : {{XXXXXXXXXXXX2}}
Match 3 : {{XXXXXXXXXXXX3}}
Match 4 : {{XXXXXXXXXXXX4}}
Match 5 : {{XXXXXXXXXXXX5}}

Thanks in advance for your help !

Have a nice day,

Anthony,

Crumbleszatan
  • 41
  • 1
  • 4

1 Answers1

0

If you want to replace {{key}} with value maby replace it like this:

var input = @"<li class='product-faq-item'>
      <p class='product-faq-title'>{{XXXXXXXXXXXX1}}</p>
      <div class='product-faq-container'>
        <p class='product-faq-text'>{{XXXXXXXXXXXX2}}<br>
          {{XXXXXXXXXXXX3}}
        </p>
      </div>
    </li>

    <li class='product-faq-item'>
      <p class='product-faq-title'>{{XXXXXXXXXXXX4}}</p>
      <div class='product-faq-container'>
        <p class='product-faq-text'>{{XXXXXXXXXXXX5}}</p>
      </div>
    </li>";

var regex = new Regex("{{.*?}}");
var dic = new Dictionary<string, object>();
dic["XXXXXXXXXXXX1"] = "X1Val";
dic["XXXXXXXXXXXX2"] = "X2Val";
dic["XXXXXXXXXXXX3"] = "X3Val";
dic["XXXXXXXXXXXX4"] = "X4Val";
dic["XXXXXXXXXXXX5"] = "X5Val";

var output = regex.Replace(input, match => $"{dic[match.Value.Replace("{", "").Replace("}", "")]}");
Maciej Kozieja
  • 1,812
  • 1
  • 13
  • 32