2011-07-24 35 views
0

我想匹配替換屬性的多個值。例如在html中爲ant buildscript匹配多個屬性值

<div class="div h1 full-width"></div> 

應產生div,h1和全角作爲單獨匹配。 我想這樣做的前綴類。因此,而不是DIV H1全寬度應該預先DIV預H1前全寬

正則表達式我有SOFAR就是

(?<=class=["'])(\b-?[_a-zA-Z]+[_a-zA-Z0-9-]*\b)+ 

這只是第一類相匹配。這是不合適的,因爲這是這種模式應該匹配的唯一的東西:(我試圖讓lookbehind採取更多,然後只是class =「但是我只是結束它採取每一步,並沒有任何東西可以取代 我想做一個模式在類屬性的引號之間單獨匹配任何值

我想爲一個Ant構建腳本處理所有文件,並用set前綴替換class =「value1 value2 value3」。在CSS文件,但你們HTML代替類的麻煩似乎很多棘手

這是一個螞蟻buildscript Java regexp package用於處理模式中使用的螞蟻標籤:。replaceregexp

上面的圖案的螞蟻implemtentation是:

<target name="prefix-class" depends=""> 
    <replaceregexp flags="g"> 
    <regexp pattern="(?&lt;=class=['&quot;])(\b-?[_a-zA-Z]+[_a-zA-Z0-9-]*\b)+"/> 
    <substitution expression=".${prefix}\1"/> 
    <fileset dir="${dest}"/> 
    </replaceregexp> 
</target>  
+0

你用什麼工具/語言來處理正則表達式? – Chris

+0

更新的問題 –

+0

姆姆...我不認爲你可以找到n(或在你的情況3)不同類的條目,並用一個簡單的正則表達式代替它們。如果你需要在螞蟻中這樣做,我認爲你必須編寫你自己的螞蟻任務。更好的方法是xslt,你熟悉xslt嗎? – Chris

回答

0

放棄螞蟻ReplaceRegExp並用XSLT排序我的問題以將xhtml轉換爲xhtml。

以下代碼爲元素類屬性的所有值添加前綴。 xhtml源文檔必須正確格式化才能解析。

<xsl:stylesheet version="2.0" 
xmlns:xhtml="http://www.w3.org/1999/xhtml" 
xmlns="http://www.w3.org/1999/xhtml" 
xmlns:fn="http://www.w3.org/2005/xpath-functions" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
exclude-result-prefixes="xhtml xsl xs"> 

    <xsl:output method="xml" version="1.0" encoding="UTF-8" 
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1.dtd" 
    indent="yes" omit-xml-declaration="yes"/> 

    <xsl:param name="prefix" select="'oo-'"/> 

    <xsl:template match="/"> 
    <xsl:apply-templates select="./@*|./node()" /> 
    </xsl:template> 

    <!--remove these atts from output, default xhtml values from dtd --> 
    <xsl:template match="xhtml:a/@shape"/> 
    <xsl:template match="@rowspan"/> 
    <xsl:template match="@colspan"/> 

    <xsl:template match="@class"> 
    <xsl:variable name="replace_regex"> 
     <xsl:value-of select="$prefix"/> 
     <xsl:text>$1</xsl:text> 
    </xsl:variable> 
    <xsl:attribute name="class"> 
     <xsl:value-of select="fn:replace(. , '(\w+)' , $replace_regex)"/> 
    </xsl:attribute> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
0

我不認爲你可以找到N(或在您的案件3)不同類的條目,並在一個簡單的正則表達式分別代替它們。如果你需要在螞蟻中這樣做,我認爲你必須編寫你自己的螞蟻任務。更好的方法是xslt,你熟悉xslt嗎?