• <bdo id='Y6n3V'></bdo><ul id='Y6n3V'></ul>

    <small id='Y6n3V'></small><noframes id='Y6n3V'>

    <i id='Y6n3V'><tr id='Y6n3V'><dt id='Y6n3V'><q id='Y6n3V'><span id='Y6n3V'><b id='Y6n3V'><form id='Y6n3V'><ins id='Y6n3V'></ins><ul id='Y6n3V'></ul><sub id='Y6n3V'></sub></form><legend id='Y6n3V'></legend><bdo id='Y6n3V'><pre id='Y6n3V'><center id='Y6n3V'></center></pre></bdo></b><th id='Y6n3V'></th></span></q></dt></tr></i><div id='Y6n3V'><tfoot id='Y6n3V'></tfoot><dl id='Y6n3V'><fieldset id='Y6n3V'></fieldset></dl></div>

      1. <tfoot id='Y6n3V'></tfoot>

      2. <legend id='Y6n3V'><style id='Y6n3V'><dir id='Y6n3V'><q id='Y6n3V'></q></dir></style></legend>

        嵌套标签的正则表达式(最里面使它更容易)

        时间:2023-07-31

          • <bdo id='Os8A3'></bdo><ul id='Os8A3'></ul>
          • <tfoot id='Os8A3'></tfoot>

            • <small id='Os8A3'></small><noframes id='Os8A3'>

                <legend id='Os8A3'><style id='Os8A3'><dir id='Os8A3'><q id='Os8A3'></q></dir></style></legend>
                  <i id='Os8A3'><tr id='Os8A3'><dt id='Os8A3'><q id='Os8A3'><span id='Os8A3'><b id='Os8A3'><form id='Os8A3'><ins id='Os8A3'></ins><ul id='Os8A3'></ul><sub id='Os8A3'></sub></form><legend id='Os8A3'></legend><bdo id='Os8A3'><pre id='Os8A3'><center id='Os8A3'></center></pre></bdo></b><th id='Os8A3'></th></span></q></dt></tr></i><div id='Os8A3'><tfoot id='Os8A3'></tfoot><dl id='Os8A3'><fieldset id='Os8A3'></fieldset></dl></div>
                    <tbody id='Os8A3'></tbody>
                  本文介绍了嵌套标签的正则表达式(最里面使它更容易)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对此进行了相当多的研究,但找不到如何将嵌套的 html 标签 属性匹配的工作示例.我知道可以匹配没有属性的平衡/嵌套最里面的标签(例如,正则表达式和将是 #<div[^>]*>(?:(?> [^<]+ ) |<(?!div[^>]*>))*?</div>#x).

                  但是,我希望看到一个正则表达式模式,它可以找到带有属性的 html 标记对.

                  例子:基本上应该匹配

                  <div class="aaa">**<div class="aaa">** <div>

                  </div>**</div>** </div>

                  而不是

                  <div class="aaa">**<div class="aaa">** <div>

                  **</div>** </div></div>

                  有人有什么想法吗?

                  出于测试目的,我们可以使用:http://www.lumadis.be/regex/test_regex.php

                  <小时>

                  PS.Steven 在他的博客中提到了一个解决方案(实际上是在评论中),但它不起作用

                  http://blog.stevenlevithan.com/archives/match-innermost-html元素

                  $regex = '/

                  解决方案

                  匹配 <div> &</div> 标签,加上它们的属性 &内容:

                  #<div(?:(?!(<div|</div>)).)*</div>#s

                  这里的关键是 (?:(?!STRING).)* 是字符串,就像 [^CHAR]* 是字符一样.

                  来源:https://stackoverflow.com/a/6996274

                  <小时>

                  PHP 中的示例:

                   $match) {回声************".
                  ".$匹配."
                  ";}

                  输出:

                  ************<div id="3">在 3</div>************<div id="5">在 5</div>

                  I researched this quite a bit, but couldn't find a working example how to match nested html tags with attributes. I know it is possible to match balanced/nested innermost tags without attributes (for example a regex for and would be #<div[^>]*>(?:(?> [^<]+ ) |<(?!div[^>]*>))*?</div>#x).

                  However, I would like to see a regex pattern that finds an html tag pair with attributes.

                  Example: It basically should match

                  <div class="aaa"> **<div class="aaa">** <div> <div> </div> **</div>** </div>
                  

                  and not

                  <div class="aaa"> **<div class="aaa">** <div> <div> **</div>** </div> </div>
                  

                  Anybody has some ideas?

                  For testing purposes we could use: http://www.lumadis.be/regex/test_regex.php


                  PS. Steven mentioned a solution in his blog (actually in a comment), but it doesn't work

                  http://blog.stevenlevithan.com/archives/match-innermost-html-element

                  $regex = '/<div[^>]+?ids*=s*"MyID"[^>]*>(?:((?:[^<]++|<(?!/?div[^>]*>))+)|(<div[^>]*>(?>(?1)|(?2))*</div>))?</div>/i';
                  

                  解决方案

                  Matching innermost matching pairs of <div> & </div> tags, plus their attributes & content:

                  #<div(?:(?!(<div|</div>)).)*</div>#s

                  The key here is that (?:(?!STRING).)* is to strings as [^CHAR]* is to characters.

                  Credit: https://stackoverflow.com/a/6996274


                  Example in PHP:

                  <?php
                  
                  $text = <<<'EOD'
                  <div id="1">
                    in 1
                    <div id="2">
                      in 2
                      <div id="3">
                        in 3
                      </div>
                    </div>
                  </div>
                  <div id="4">
                    in 4
                    <div id="5">
                      in 5
                    </div>
                  </div>
                  EOD;
                  
                  $matches = array();
                  preg_match_all('#<div(?:(?!(<div|</div>)).)*</div>#s', $text, $matches);
                  
                  foreach ($matches[0] as $index => $match) {
                    echo "************" . "
                  " . $match . "
                  ";
                  }
                  

                  Outputs:

                  ************
                  <div id="3">
                        in 3
                      </div>
                  ************
                  <div id="5">
                      in 5
                    </div>
                  

                  这篇关于嵌套标签的正则表达式(最里面使它更容易)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <tfoot id='3SbiX'></tfoot>
                  • <small id='3SbiX'></small><noframes id='3SbiX'>

                  • <legend id='3SbiX'><style id='3SbiX'><dir id='3SbiX'><q id='3SbiX'></q></dir></style></legend>
                        <tbody id='3SbiX'></tbody>

                      1. <i id='3SbiX'><tr id='3SbiX'><dt id='3SbiX'><q id='3SbiX'><span id='3SbiX'><b id='3SbiX'><form id='3SbiX'><ins id='3SbiX'></ins><ul id='3SbiX'></ul><sub id='3SbiX'></sub></form><legend id='3SbiX'></legend><bdo id='3SbiX'><pre id='3SbiX'><center id='3SbiX'></center></pre></bdo></b><th id='3SbiX'></th></span></q></dt></tr></i><div id='3SbiX'><tfoot id='3SbiX'></tfoot><dl id='3SbiX'><fieldset id='3SbiX'></fieldset></dl></div>
                          <bdo id='3SbiX'></bdo><ul id='3SbiX'></ul>