Gradle/Java替换文本

per*_*rak 3 java regex gradle

是否可以使用正则表达式在css文件中转换URL,方式如下所示?我不想硬编码字体名称.

从:

url('../fonts/glyphiconshalflings-regular.eot?#iefix') format('embedded-opentype')
Run Code Online (Sandbox Code Playgroud)

至:

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

这就是我现在所拥有的:

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")
    }
}
Run Code Online (Sandbox Code Playgroud)

Son*_*Gao 5

假设你只想格式化url(something),这应该做你想要的:

String regex = "url\\('([^']*?\\.(eot|ttf|fnt))(.*?)'\\)";
//                   font file formats^           
content = content.replaceAll(regex, "url(\"#{resource['css:$1']}$3\")");
Run Code Online (Sandbox Code Playgroud)

编辑:忘了逃避一些角色.