2

<p>文字列(テスト用)があり、divの下のすべてのインスタンスを置き換えたい<div id="text">. それ、どうやったら出来るの ?

m修飾子と修飾子でテストしましsたが、無駄でした (最初のものだけが置き換えられます)。以下にPerlコードを示します。

#!/usr/bin/perl
use strict;
use warnings;

my $string = <<STRING;
<div id="main">
    hellohello
    <div id="text">
        nokay.
        <p>This is p1, SHUD B replaced</p>
        Alright
        <p>This is p2, SHUD B replaced</p>
        Yes 2
        <p>this is P3, SHUD B replaced</p>
        Okay done
        bye
    </div>
    bye
    <p>this is not under the div whose id is text and SHUDN'T b replaced</p>
</div>

STRING

my $str_bak = $string;
print "Sring is : \n$string\n\n";

$string =~ s/(<div id="text">.*?)<p>(.*)(<\/p>.*?<\/div>)/$1<p style="text-align:left;">$2 $3/sig;

print "Sring now is : \n$string\n\n";
4

4 に答える 4

2

XML::XSH2 の使用:

open :F html 1.html ;
for //div[@id="text"]/p
    set @style "text-align:left;" ;
save :b ;
于 2012-05-28T09:35:57.627 に答える
0

これを試して

(?is)<p>.+?</p>(?=.*?</div>)

コード

$subject =~ s!(?is)<p>.+?</p>(?=.*?</div>)!!g;

説明

"
(?is)        # Match the remainder of the regex with the options: case insensitive (i); dot matches newline (s)
<p>          # Match the characters “&lt;p>” literally
.            # Match any single character
   +?           # Between one and unlimited times, as few times as possible, expanding as needed (lazy)
</p>         # Match the characters “&lt;/p>” literally
(?=          # Assert that the regex below can be matched, starting at this position (positive lookahead)
   .            # Match any single character
      *?           # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
   </div>       # Match the characters “&lt;/div>” literally
)
"

アップデート

次のようにコードを変更します。

#!/usr/bin/perl
use strict;
use warnings;

my $string = <<STRING;
<div id="main">
    hellohello
    <div id="text">
        nokay.
        <p>This is p1, SHUD B replaced</p>
        Alright
        <p>This is p2, SHUD B replaced</p>
        Yes 2
        <p>this is P3, SHUD B replaced</p>
        Okay done
        bye
    </div>
    bye
    <p>this is not under the div whose id is text and SHUDN'T b replaced</p>
</div>

STRING

my $str_bak = $string;
print "Sring is : \n$string\n\n";

$string =~ s!(?is)<p>.+?</p>(?=.*?</div>)!!g;;

print "Sring now is : \n$string\n\n";

そして、そのスクリプトは正確に何のために構築されているかを示します。<p>内の要素を除くすべてのコンテンツを表示していますdiv

于 2012-05-28T08:15:55.560 に答える
0

助けてくれてありがとう。

そのための正規表現を見つけることができました。だから私は「回避策」でそれをしました。こうやって :

while( $val =~ s/(<div id="article">.*?)<p>/$1<p style="text-align:left;">/sig )
{  }

したがって、基本的にその正規表現は最初の一致にのみ適用され、それが空の while ループで繰り返されるようにする理由です (置換する一致がなくなると、ループは終了します)。

于 2012-05-31T05:02:17.087 に答える
0

最初に、この投稿で説明されているトリックを使用したと言う必要があります Perlで変数として正規表現置換を渡す?

#!/usr/bin/perl
use strict;
use warnings;

my $string = <<STRING;
<div id="main">
    hellohello
    <div id="text">
        nokay.
        <p>This is p1, SHUD B replaced</p>
        Alright
        <p>This is p2, SHUD B replaced</p>
        Yes 2
        <p>this is P3, SHUD B replaced</p>
        Okay done
        bye
    </div>
    bye
    <p>this is not under the div whose id is text and SHUDN'T b replaced</p>
</div>

STRING

my $str_bak = $string;
print "Sring is : \n$string\n\n";

$string =~ s/(<div id="text">.*?)<p>(.*)(<\/p>.*?<\/div>)/$1<p style="text-align:left;">$2 $3/sig;

sub modify
{
  my($text, $code) = @_;
  $code->($text);
  return $text;
}

my $new_text = modify($string, sub {
    my $div = '(<div id="text">.*?</div>)';
    $string =~ m#$div#is;
    my $found = $1;
print "found : \n$found\n\n";
    my $repl = modify ($found, sub {
         $_[0] =~ s/<p>/<p style="text-align:left;">/g
    }) ;
    $_[0] =~ s/$found/$repl/ 
});

print "Result : \n$new_text\n\n";

秘訣は、変更サブを使用して、テキストの高次処理を許可することです。次に、 を分離し、その上に<div id="text">...</div>の置換を適用できます<p>

于 2012-05-28T09:19:19.403 に答える