2010-12-03 5 views
0

Je html qui ressembleregex: tout supprimer mais?

<tr> 
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=Yellow">Yellow</a>&nbsp;</td> 
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=FFFF00">#FFFF00</a></td> 
<td bgcolor="#FFFF00">&nbsp;</td> 
<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=FFFF00">Shades</a></td> 
<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=FFFF00&colortop=FFFFFF">Mix</a></td> 
</tr> 


<tr> 
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=YellowGreen">YellowGreen</a>&nbsp;</td> 
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=9ACD32">#9ACD32</a></td> 
<td bgcolor="#9ACD32">&nbsp;</td> 
<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=9ACD32">Shades</a></td> 
<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=9ACD32&colortop=FFFFFF">Mix</a></td> 
</tr> 

Ce que je voulais faire est

filtre le code html donc je ne finissent avec

<td bgcolor="#XXXXXX">&nbsp;</td>

Puis filtre que je finis donc avec tout un tas de rangées de

XXXXXX 
XXXXXX 

Comment ferais-je cela?

+0

Vous ne voulez pas. C'est un endroit terrible pour utiliser n'importe quelle regex ... D'autres possibilités? – Blender

+0

pagination dr. Bobince? http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – harpo

+0

3 983 personnes sont d'accord: [Ne pas parser HTML avec regex!] (http : //stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) –

Répondre

1

Salut, vous pouvez utiliser suivant regex.

\<td bgcolor\=\"([^\"]+\)">\&nbsp\;\<\/td\> 

Utilisez l'option de groupe pour capturer "xxxxxx"

0

Première regex pour correspondre les balises droite:

\<td bgcolor="#[0-9A-Fa-f]{6}">&nbsp;\</td\> 

Ensuite, vous pouvez filtrer les données à nouveau avec (ou utiliser une option de groupe, dépend de langue à qui est plus pratique):

[0-9A-Fa-f]{6} 

C'est, si vous voulez utiliser regex (ne pas me tirer dessus, la question est ce que l'expression régulière puis-je utiliser pour cela)

0

si vous devez utiliser regex, voici une démonstration en utilisant le RIR Ruby:

>> %Q{ 
<tr> 
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=Yellow">Yellow</a>&nbsp;</td> 
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=FFFF00">#FFFF00</a></td> 
<td bgcolor="#FFFF00">&nbsp;</td> 
<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=FFFF00">Shades</a></td> 
<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=FFFF00&colortop=FFFFFF">Mix</a></td> 
</tr> 


<tr> 
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=YellowGreen">YellowGreen</a>&nbsp;</td> 
<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=9ACD32">#9ACD32</a></td> 
<td bgcolor="#9ACD32">&nbsp;</td> 
<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=9ACD32">Shades</a></td> 
<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=9ACD32&colortop=FFFFFF">Mix</a></td> 
</tr> 
}.scan(/<td[^>]*>&nbsp;<\/td>/).map {|s| s[/#([a-f0-9]+)/i, 1]} 

=> ["FFFF00", "9ACD32"] 
0

Je n'analyser soit de HTML avec regex, mais si je le faisais je le ferais comme ceci;)

var html = '<tr>\n<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=Yellow">Yellow</a>&nbsp;</td>\n<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=FFFF00">#FFFF00</a></td>\n<td bgcolor="#FFFF00">&nbsp;</td>\n<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=FFFF00">Shades</a></td>\n<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=FFFF00&colortop=FFFFFF">Mix</a></td>\n</tr>\n\n\n<tr>\n<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?color=YellowGreen">YellowGreen</a>&nbsp;</td>\n<td align="left"><a target="_blank" href="/tags/ref_color_tryit.asp?hex=9ACD32">#9ACD32</a></td>\n<td bgcolor="#9ACD32">&nbsp;</td>\n<td align="left"><a href="/tags/ref_colorpicker.asp?colorhex=9ACD32">Shades</a></td>\n<td align="left"><a href="/tags/ref_colormixer.asp?colorbottom=9ACD32&colortop=FFFFFF">Mix</a></td>\n</tr>' 
     .split('\n'),  
    colors = [], 
    i, l, 
    match; 

for(i = 0, l = html.length; i < l; i++) { 
    if(match = /<td bgcolor="#([\da-fA-F]{6})">&nbsp;<\/td>/.exec(html[i])) { 
     colors.push(match[1]); 
    } 
} 

console.log(colors); 
Questions connexes