我有这个正则表达式:
url\(\s*(['"]?+)(.*?)\1\s*\)
从 css 文件中检索所有 url。
我使用这段代码,但是我得到一个错误:
Pattern p = Pattern.compile("url\(\s*(['\"]?+)(.*?)\1\s*\)");
Matcher m = p.matcher(cssText);
while (m.find()) {
println m.group()
}
正如您所注意到的,我将 \
添加到 esacpe "
中。但是徒劳无功
最佳答案
您需要在 Java 正则表达式中使用 \\
而不是 \
:
Pattern p = Pattern.compile( "url\\(\\s*(['\"]?+)(.*?)\\1\\s*\\)" );
- 第一个
\
用于从 String 对象中转义 - 第二个
\
用于从底层正则表达式引擎转义
关于java - 如何使用正则表达式包含“with Pattern.compile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26571151/