使用正则表达式在td标记之间获取内容

时间:2012-03-20 15:57:09

标签: regex coldfusion

以下是样本数据

<table class="sparql" border="1"> 
<tr> <th>abstract</th></tr> 
<tr> 
  <td>
    Cologne is Germany&#39;s fourth-largest city, and is the 
    largest city both in 
    the German Federal State of North Rhine-Westphalia and within the 
    Rhine-Ruhr Metropolitan Area, one of the major European metropolitan
    areas with more than ten million inhabitants."@en
 </td> 
 </tr> 
</table>

我试图使用正则表达式在<td>标记之间获取内容。我试过像

这样的东西
<td>.*</td>

但是如何放弃tags itselef?

1 个答案:

答案 0 :(得分:2)

正如@MisterJack指出的那样,你需要使用子表达式才能引用匹配。如果您使用的是REReplace(),那么您可以使用\1(或\2等)作为匹配的反向引用。如果您使用的是REFind(),那么您需要将其与returnsubexpressions=true一起使用,它会返回structlenpos数组匹配值。我会这样做:

<!--- I use "?" below because we want to be lazy rather than greedy --->
<cfset the_match = REFind(the_content, "<td>(.*?)</td>", 1, true) />

<cfdump var="#the_match#" />

您应该会看到包含lenpos数组的结构。它可能在每个数组中只有一个元素。为了获得匹配内容,您可以这样做:

<cfset match_content = mid(the_content, pos[i], len[i]) />

希望这有帮助。