2010-10-28 27 views
2

我有一個HTML字符串,像這樣:正則表達式來分割HTML標籤

<img src="http://foo"><img src="http://bar"> 

什麼是正則表達式模式來此分拆爲兩家獨立的img標籤?

+5

它們已經是2個單獨的標籤 – JaredPar 2010-10-28 16:21:33

+2

它已經是兩個單獨的'img'標籤。 – Welbog 2010-10-28 16:21:41

+0

請搜索相似的問題。他們有很多。除非您有非常小的,特定的和模式化的輸入,否則不要使用RegEx for HTML。 – Viet 2010-10-28 16:30:41

回答

5

Don't do it with regex。使用HTML/XML解析器。你甚至可以先通過Tidy來清理它。大多數語言都有一個Tidy庫。你在用什麼語言?

2

這將做到這一點:

<img\s+src=\"[^\"]*?\"> 

或者你也可以做到這一點,以考慮任何附加屬性

<img\s+[^>]*?\bsrc=\"[^\"]*?\"[^>]*> 
+0

這沒有考慮,你說它「附加屬性」。看看我的解決方案如何正確執行此操作。那麼,如果不使用HTML解析類,就儘可能地正確。 – tchrist 2010-10-28 18:08:46

+0

我其實是在尋找一個快速和骯髒的解決方案來獲得字符串中的img標籤的所有src屬性值和碰到這個答案,這是非常有益的,對我來說,我只需要添加兩個支架:'] *? \ BSRC = \ 「([^ \」] *?)\「[^>] *>' – Nick 2013-11-28 15:16:45

0
<img src=\"https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?\"> 

PHP例子:

$prom = '<img src="http://foo"><img src="http://bar">'; 

preg_match_all('|<img src=\"https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?\">|',$prom, $matches); 

print_r($matches[0]); 
7

如何確定是你說你的字符串是exac tly那?那麼輸入如下:

<img alt=">"   src="http://foo" > 
<img src='http://bar' alt='<'   > 

這是什麼編程語言?有沒有理由不使用標準的HTML解析類來處理這個問題?正則表達式只是一個很好的方法,當你有一套非常有名的輸入。它們不適用於真正的HTML,僅適用於受操縱的演示。

即使你必須使用正則表達式,你應該使用正確的語法之一。這很容易。我已經在zillion網頁上測試了以下programacita。它負責處理上面概述的案例 - 還有一兩個案例。

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

my $img_rx = qr{ 

    # save capture in $+{TAG} variable 
    (?<TAG> (?&image_tag)) 

    # remainder is pure declaration 
    (?(DEFINE) 

     (?<image_tag> 
      (?&start_tag) 
      (?&might_white) 
      (?&attributes) 
      (?&might_white) 
      (?&end_tag) 
     ) 

     (?<attributes> 
      (?: 
       (?&might_white) 
       (?&one_attribute) 
      ) * 
     ) 

     (?<one_attribute> 
      \b 
      (?&legal_attribute) 
      (?&might_white) = (?&might_white) 
      (?: 
       (?&quoted_value) 
       | (?&unquoted_value) 
      ) 
     ) 

     (?<legal_attribute> 
      (?: (?&required_attribute) 
       | (?&optional_attribute) 
       | (?&standard_attribute) 
       | (?&event_attribute) 
       # for LEGAL parse only, comment out next line 
       | (?&illegal_attribute) 
      ) 
     ) 

     (?<illegal_attribute> \b \w+ \b) 

     (?<required_attribute> 
      alt 
      | src 
     ) 

     (?<optional_attribute> 
      (?&permitted_attribute) 
      | (?&deprecated_attribute) 
     ) 

     # NB: The white space in string literals 
     #  below DOES NOT COUNT! It's just 
     #  there for legibility. 

     (?<permitted_attribute> 
      height 
      | is map 
      | long desc 
      | use map 
      | width 
     ) 

     (?<deprecated_attribute> 
      align 
      | border 
      | hspace 
      | vspace 
     ) 

     (?<standard_attribute> 
      class 
      | dir 
      | id 
      | style 
      | title 
      | xml:lang 
     ) 

     (?<event_attribute> 
      on abort 
      | on click 
      | on dbl click 
      | on mouse down 
      | on mouse out 
      | on key down 
      | on key press 
      | on key up 
     ) 

     (?<unquoted_value> 
      (?&unwhite_chunk) 
     ) 

     (?<quoted_value> 
      (?<quote> ["']  ) 
      (?: (?! \k<quote>) .) * 
      \k<quote> 
     ) 

     (?<unwhite_chunk> 
      (?: 
       # (?! [<>'"]) 
       (?! >) 
       \S 
      ) + 
     ) 

     (?<might_white>  \s * ) 

     (?<start_tag> 
      < (?&might_white) 
      img 
      \b  
     ) 

     (?<end_tag>   
      (?&html_end_tag) 
      | (?&xhtml_end_tag) 
     ) 

     (?<html_end_tag>  > ) 
     (?<xhtml_end_tag> /> ) 

    ) 

}six; 

$/ = undef; 
$_ = <>; # read all input 

# strip stuff we aren't supposed to look at 
s{ <! DOCTYPE .*?   > }{}sx; 
s{ <! \[ CDATA \[ .*? \]\] > }{}gsx; 

s{ <script> .*? </script> }{}gsix; 
s{ <!--  .*?  --> }{}gsx; 

my $count = 0; 

while (/$img_rx/g) { 
    printf "Match %d at %d: %s\n", 
      ++$count, pos(), $+{TAG}; 
} 

你走了。什麼都沒有!

哎,爲什麼你會曾經想要使用HTML解析類,給定了在正則表達式中如何處理HTML。 ☺

+1

哇。只需哇.. – Soaku 2017-08-31 14:56:18

0

一個稍微瘋狂/輝煌/奇怪的做法是將其拆分> <,然後將兩個字符分別添加回分割後的字符串。

$string = '<img src="http://foo"><img src="http://bar">'; 
$KimKardashian = split("><",$string); 
$First = $KimKardashian[0] . '>'; 
$Second = '<' . $KimKardashian[1];