gpt4 book ai didi

java - 在java中使用正则表达式删除匹配的字符串

转载 作者:行者123 更新时间:2023-11-29 08:04:22 25 4
gpt4 key购买 nike

这是我的代码,请检查。最后我想删除 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com