2014-01-29 46 views
0

我使用下面的代碼:獲取之間>和擺脫空間的<

$page_entire_code =~ s/> +?</></g; 

刪除><之間的空間在我的HTML網頁。但是,我注意到它弄亂了我的網頁的麪包屑。例如:

<div id="breadcrumb" itemprop="breadcrumb"> 
    <b> 
    You are here: <a href="http://www.romancestuck.com/">RomanceStuck</a> > <a href="http://www.romancestuck.com/marriage/love-and-marriage.htm">Marriage</a> > 11 Tips for Improving a Strained Relationship 
    </b> 
</div> 

被壓縮到:

<div id="breadcrumb" itemprop="breadcrumb"><b>You are here: <a href="http://www.romancestuck.com/">RomanceStuck</a> ><a href="http://www.romancestuck.com/marriage/love-and-marriage.htm">Marriage</a> > 11 Tips for Improving a Strained Relationship</b></div> 

>RomanceStuck鏈接後沒有空格就等之後它應該。我怎樣才能改變我的Perl替代線,以便它不會弄亂我的麪包屑?我想也許我可以說替換>之後的任何字符除了空格。

謝謝!

+3

您的麪包屑是錯誤的。你應該使用'>'。 –

+0

我認爲你可以使用&gt或&lt;大於和小於符號,這將允許你的空間,如你所願。 –

+0

您提供的信息不足以回答您的問題。如果您告訴我們您爲什麼要篡改變量中的HTML內容,然後嘗試從中過濾空格,您可能會收到一條建議。你從哪裏得到這個HTML內容?如果我們建議對該內容進行一些更改,您是否有控制權? – RaviH

回答

1

我想你可以通過&gt;在HTML代碼中

<div id="breadcrumb" itemprop="breadcrumb"> 
    <b> 
    You are here: <a href="http://www.romancestuck.com/">RomanceStuck</a> &gt; <a href="http://www.romancestuck.com/marriage/love-and-marriage.htm">Marriage</a> &gt; 11 Tips for Improving a Strained Relationship 
    </b> 
</div> 
2
<div id="breadcrumb" itemprop="breadcrumb"> 
    <b> 
    You are here: <a href="http://www.romancestuck.com/">RomanceStuck</a> &gt; <a href="http://www.romancestuck.com/marriage/love-and-marriage.htm">Marriage</a> &gt; 11 Tips for Improving a Strained Relationship 
    </b> 
</div> 

&gt;

0

此正則表達式可能取代>取代>:/>(\ s | \ n)的(< | [aZ])/ 替換爲空白字符。它應該刪除結束標記和打開一個或任何文本之間的空白或行返回。

0

「撤換>後,除了一個空間中的任何字符來了」你會怎麼做?

$page_entire_code =~ s/([^ ]>) +?</$1</g; 

或更近的皮爾斯,

$page_entire_code =~ s/[^ ]>\K +?</</g; 

(雖然有沒用;你匹配所有空格,最大爲<;如果可能,說明比較少的比較沒有意義。)

+0

$ page_entire_code =〜s /([^]>)+? WebStuck