0

次のようなhtmlコードがあります。

<a href="The Whole World">

そして、それを次のように見せたい:

<a href="TheWholeWorld">

パールを使用。どうすればいいですか?ありがとう!

4

2 に答える 2

0

ショートコードフラグメント

$a='<a href="the whole world">';
($c=$a)=~s/("\S+|\S+|")\s*/$1/g;
print $c;

正規表現の仕組み:

s/("\S+|\S+|")\s*/$1/g;
      ^ ^  ^      ^   ^  ^
      + +  +      +   +  +-- global flag, apply repeatedly
      | |  |      |   +-- substitute in the first capture group
      | |  |      +-- white space, but outside of the capture group
      | |  +-- | alternative operator
      | +-- \S+ match any non zero amount of non white space
      +-- start capturing group

そのため、内部の空白以外を見つけて"、キャプチャ グループに入れます。

各単語間の空白がキャプチャ グループに含まれない

これは繰り返し発生し、キャプチャ グループは結果にコピーされますが、空白はコピーされません。

長期的には保守が容易になるため、xml フラグメントに対してパーサーを使用することをお勧めします。

于 2013-07-23T08:14:32.483 に答える