2013-06-29 28 views
1

我有一個名稱和URL的表是這樣的:在HTML表格中選擇URL與正則表達式

<tr> 
    <td>name1</td> 
    <td>www.url.com</td> </tr> 
<tr> 
    <td>name2</td> 
<td>www.url2.com</td> </tr> 

我要選擇表中的所有URL的資料表。 我想:

<td>w{3,3}.*(</td>){1,1} 

但這種表達不「停」在第一</td>。我得到:

<td>www.url.com</td> </tr> 
    <tr> 
    <td>name2</td> 
    <td>www.url2.com</td> 

作爲結果。我的錯誤在哪裏?

回答

1

有幾種匹配URL的方法。我會嘗試最簡單的你的需求:只是糾正你的正則表達式。你可以用這個來代替一個:

<td>w{3}.*?</td> 

說明:

<td>   # this part is ok 
w{3,3}  # the notation {3} is simpler for this case and has the same effect 
.*   # the main problem: you have to use .*? to make .* non-greedy, that 
       is, to make it match as little as possible 
(</td>){1,1} # same as second line. As the number is 1, {1} is not needed 
+0

注意:如果你只是想匹配正好的URL部分**(沒有'td's),你可以使用後面的一個和前面的代碼:'(?​​)w {3}。*?( ?=)' – acdcjunior

+0

謝謝你的解釋。它很棒! – user2494904

0

你的正則表達式可以

\b(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|] 

"((((ht{2}ps?://)?)((w{3}\\.)?))?)[^.&&[a-zA-Z0-9]][a-zA-Z0-9.-]+[^.&&[a-zA-Z0-9]](\\.[a-zA-Z]{2,3})" 

請參閱此鏈接 - What is the best regular expression to check if a string is a valid URL?。許多答案是可用的。

+0

THX爲您快速回復。我已經嘗試過這個正則表達式。 Notepad ++說他可以找到這個正則表達式..我該怎麼辦? – user2494904