Gradle/Java替换文本

3

有没有可能使用正则表达式来转换CSS文件中的URL,就像下面示例的方式一样?我不想硬编码字体名称。

从:

url('../fonts/glyphiconshalflings-regular.eot?#iefix') format('embedded-opentype')

to:

url("#{resource['css:../fonts/glyphiconshalflings-regular.eot']}?#iefix") format('embedded-opentype')

这是我目前拥有的内容:

task fixCSSResources << {
    FileTree cssFiles = fileTree("src/main/webapp") {
        include "**/*.css"
    }
    cssFiles.each { File cssFile ->
        String content = cssFile.getText("UTF-8")
        // what to do here?
        cssFile.write(content, "UTF-8")
    }
}

文本中有什么模式?它是否总是url(something) format(something)的格式? - Song Gao
@SongCGGao 这是一个例子 http://pastebin.com/VzmaFrMJ - perak
1个回答

5
假设您只想格式化url(something),那么以下代码应该可以满足您的需求:
String regex = "url\\('([^']*?\\.(eot|ttf|fnt))(.*?)'\\)";
//                   font file formats^           
content = content.replaceAll(regex, "url(\"#{resource['css:$1']}$3\")");

编辑:忘记转义某些字符。


1
你在这里指定文件的位置是哪里? - IgorGanapolsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接