这是我的代码,请检查。最后我想删除 list-style-image: url(images/dot.gif);来自字符串
String temp = "font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: F143F;list-style-image: url(images/dot.gif);list-style-type: none;";
Pattern pxPattern = Pattern.compile("([a-z]+-)+([a-z]+):(\\s)url\\(.*?\\);");
Matcher pxMatcher = pxPattern.matcher(temp);
while(pxMatcher.find()) {
System.out.println(pxMatcher.group());
String urlString =pxMatcher.group();
if(!urlString.matches("http://|https://")) {
System.out.println("Firts: "+temp.trim());
System.out.println(urlString);
System.out.println(temp.replaceAll(urlString, ""));
//System.out.println("Remove: "+temp);
}
}
最佳答案
我将删除 list-style-image
如下(而不是使用 while 循环,这可以在一行中完成):
temp.replaceAll("list-style-image:[^;]+;?", "");
解释:
- 这将查找
list-style-image
, - 然后是一个或多个不是分号的字符
- 然后是一个可选的分号
这将从字符串的中间和结尾删除 list-style-image
属性。
结果:
font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: F143F;list-style-type: none;
关于java - 在java中使用正则表达式删除匹配的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12351542/