正则表达式 – Perl替换嵌套块正则表达式

前端之家收集整理的这篇文章主要介绍了正则表达式 – Perl替换嵌套块正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在哈希数组或哈希树中获取嵌套块,以便能够用动态内容替换块.我需要替换之间的代码
  1. <!--block:XXX-->

和第一个结束块

  1. <!--endblock-->

用我的动态内容.

我有这个代码,找到一个级别的注释块但不嵌套:

  1. #<!--block:listing-->... html code block here ...<!--endblock-->
  2. $blocks{$1} = $2 while $content =~ /<!--block:(.*?)-->((?:(?:(?!<!--(.*?)-->).)|(?R))*?)<!--endblock-->/igs;

这是我想要处理的完整嵌套html模板.所以我需要找到并替换内部块“block:third”并将其替换为我的内容,然后找到“block:second”并替换它然后找到外部块“block:first”并替换它.请注意,可以有任意数量的嵌套块,而不是像下面的示例那样只有三个,它可以是几个嵌套块.

  1. use Data::Dumper;
  2.  
  3. $content=<<HTML;
  4. some html content here
  5.  
  6. <!--block:first-->
  7. some html content here
  8.  
  9. <!--block:second-->
  10. some html content here
  11.  
  12. <!--block:third-->
  13. some html content here
  14. <!--endblock-->
  15.  
  16. some html content here
  17. <!--endblock-->
  18.  
  19. some html content here
  20. <!--endblock-->
  21. HTML
  22.  
  23. $blocks{$1} = $2 while $content =~ /<!--block:(.*?)-->((?:(?:(?!<!--(.*?)-->).)|(?R))*?)<!--endblock-->/igs;
  24. print Dumper(%blocks);

所以我可以访问和修改块,比如$block {first} =“我的内容在这里”和$block {second} =“另一个内容在这里”然后替换块.

我创建了这个regex

我要补充一点.这符合我以前的答案,但稍微多一些
完成,我不想再回答这个问题了.

这是针对@daliaessam的,是对@Miller轶事的递归解析的具体回应
使用正则表达式.

只有3个部分需要考虑.所以,使用我之前的表现形式,我向你们展示了一个
关于如何做到这一点的模板.它并不像你想象的那么难.

干杯!

  1. # //////////////////////////////////////////////////////
  2. # // The General Guide to 3-Part Recursive Parsing
  3. # // ----------------------------------------------
  4. # // Part 1. CONTENT
  5. # // Part 2. CORE
  6. # // Part 3. ERRORS
  7.  
  8. (?is)
  9.  
  10. (?:
  11. ( # (1),Take off CONTENT
  12. (?&content)
  13. )
  14. | # OR
  15. (?> # Start-Delimiter (in this case,must be atomic because of .*?)
  16. <!--block:
  17. ( .*? ) # (2),Block name
  18. -->
  19. )
  20. ( # (3),Take off The CORE
  21. (?&core)
  22. |
  23. )
  24. <!--endblock--> # End-Delimiter
  25.  
  26. | # OR
  27. ( # (4),Take off Unbalanced (delimeter) ERRORS
  28. <!--
  29. (?: block: .*? | endblock )
  30. -->
  31. )
  32. )
  33.  
  34. # ///////////////////////
  35. # // Subroutines
  36. # // ---------------
  37.  
  38. (?(DEFINE)
  39.  
  40. # core
  41. (?<core>
  42. (?>
  43. (?&content)
  44. |
  45. (?> <!--block: .*? --> )
  46. # recurse core
  47. (?:
  48. (?&core)
  49. |
  50. )
  51. <!--endblock-->
  52. )+
  53. )
  54.  
  55. # content
  56. (?<content>
  57. (?>
  58. (?!
  59. <!--
  60. (?: block: .*? | endblock )
  61. -->
  62. )
  63. .
  64. )+
  65. )
  66.  
  67. )

Perl代码

  1. use strict;
  2. use warnings;
  3.  
  4. use Data::Dumper;
  5.  
  6. $/ = undef;
  7. my $content = <DATA>;
  8.  
  9. # Set the error mode on/off here ..
  10. my $BailOnError = 1;
  11. my $IsError = 0;
  12.  
  13. my $href = {};
  14.  
  15. ParseCore( $href,$content );
  16.  
  17. #print Dumper($href);
  18.  
  19. print "\n\n";
  20. print "\nBase======================\n";
  21. print $href->{content};
  22. print "\nFirst======================\n";
  23. print $href->{first}->{content};
  24. print "\nSecond======================\n";
  25. print $href->{first}->{second}->{content};
  26. print "\nThird======================\n";
  27. print $href->{first}->{second}->{third}->{content};
  28. print "\nFourth======================\n";
  29. print $href->{first}->{second}->{third}->{fourth}->{content};
  30. print "\nFifth======================\n";
  31. print $href->{first}->{second}->{third}->{fourth}->{fifth}->{content};
  32. print "\nSix======================\n";
  33. print $href->{six}->{content};
  34. print "\nSeven======================\n";
  35. print $href->{six}->{seven}->{content};
  36. print "\nEight======================\n";
  37. print $href->{six}->{seven}->{eight}->{content};
  38.  
  39. exit;
  40.  
  41.  
  42. sub ParseCore
  43. {
  44. my ($aref,$core) = @_;
  45. my ($k,$v);
  46. while ( $core =~ /(?is)(?:((?&content))|(?><!--block:(.*?)-->)((?&core)|)<!--endblock-->|(<!--(?:block:.*?|endblock)-->))(?(DEFINE)(?<core>(?>(?&content)|(?><!--block:.*?-->)(?:(?&core)|)<!--endblock-->)+)(?<content>(?>(?!<!--(?:block:.*?|endblock)-->).)+))/g )
  47. {
  48. if (defined $1)
  49. {
  50. # CONTENT
  51. $aref->{content} .= $1;
  52. }
  53. elsif (defined $2)
  54. {
  55. # CORE
  56. $k = $2; $v = $3;
  57. $aref->{$k} = {};
  58. # $aref->{$k}->{content} = $v;
  59. # $aref->{$k}->{match} = $&;
  60.  
  61. my $curraref = $aref->{$k};
  62. my $ret = ParseCore($aref->{$k},$v);
  63. if ( $BailOnError && $IsError ) {
  64. last;
  65. }
  66. if (defined $ret) {
  67. $curraref->{'#next'} = $ret;
  68. }
  69. }
  70. else
  71. {
  72. # ERRORS
  73. print "Unbalanced '$4' at position = ",$-[0];
  74. $IsError = 1;
  75.  
  76. # Decide to continue here ..
  77. # If BailOnError is set,just unwind recursion.
  78. # -------------------------------------------------
  79. if ( $BailOnError ) {
  80. last;
  81. }
  82. }
  83. }
  84. return $k;
  85. }
  86.  
  87. #================================================
  88. __DATA__
  89. some html content here top base
  90. <!--block:first-->
  91. <table border="1" style="color:red;">
  92. <tr class="lines">
  93. <td align="left" valign="<--valign-->">
  94. <b>bold</b><a href="http://www.mewsoft.com">mewsoft</a>
  95. <!--hello--> <--again--><!--world-->
  96. some html content here 1 top
  97. <!--block:second-->
  98. some html content here 2 top
  99. <!--block:third-->
  100. some html content here 3 top
  101. <!--block:fourth-->
  102. some html content here 4 top
  103. <!--block:fifth-->
  104. some html content here 5a
  105. some html content here 5b
  106. <!--endblock-->
  107. <!--endblock-->
  108. some html content here 3a
  109. some html content here 3b
  110. <!--endblock-->
  111. some html content here 2 bottom
  112. <!--endblock-->
  113. some html content here 1 bottom
  114. <!--endblock-->
  115. some html content here1-5 bottom base
  116.  
  117. some html content here 6-8 top base
  118. <!--block:six-->
  119. some html content here 6 top
  120. <!--block:seven-->
  121. some html content here 7 top
  122. <!--block:eight-->
  123. some html content here 8a
  124. some html content here 8b
  125. <!--endblock-->
  126. some html content here 7 bottom
  127. <!--endblock-->
  128. some html content here 6 bottom
  129. <!--endblock-->
  130. some html content here 6-8 bottom base

输出>>

  1. Base======================
  2. some html content here top base
  3.  
  4. some html content here1-5 bottom base
  5.  
  6. some html content here 6-8 top base
  7.  
  8. some html content here 6-8 bottom base
  9.  
  10. First======================
  11.  
  12. <table border="1" style="color:red;">
  13. <tr class="lines">
  14. <td align="left" valign="<--valign-->">
  15. <b>bold</b><a href="http://www.mewsoft.com">mewsoft</a>
  16. <!--hello--> <--again--><!--world-->
  17. some html content here 1 top
  18.  
  19. some html content here 1 bottom
  20.  
  21. Second======================
  22.  
  23. some html content here 2 top
  24.  
  25. some html content here 2 bottom
  26.  
  27. Third======================
  28.  
  29. some html content here 3 top
  30.  
  31. some html content here 3a
  32. some html content here 3b
  33.  
  34. Fourth======================
  35.  
  36. some html content here 4 top
  37.  
  38.  
  39. Fifth======================
  40.  
  41. some html content here 5a
  42. some html content here 5b
  43.  
  44. Six======================
  45.  
  46. some html content here 6 top
  47.  
  48. some html content here 6 bottom
  49.  
  50. Seven======================
  51.  
  52. some html content here 7 top
  53.  
  54. some html content here 7 bottom
  55.  
  56. Eight======================
  57.  
  58. some html content here 8a
  59. some html content here 8b

猜你在找的正则表达式相关文章