字符串模糊匹配的实现

Jay_+wqq_635731323 2012-09-17 01:03:53
有如下需求:
对于一段字符串内容,如:
@font-face {
font-family: UKK_Basma;
font-style: normal;
font-weight: normal;
src: url("ZNJK_Basma.ttf");
}
@font-face {
font-family: UKK_Basma;
font-style: normal;
font-weight: normal;
src: url("UKK_Basma.ttf");
}
@font-face {
font-family: UKK_Basma;
font-style: normal;
font-weight: normal;
src: url("UKK_BasmaB.ttf");
}

把红色部分全部替换成一个统一的字符串,如src: url("myfont.ttf");

大致了解可以用通配符,但是没有学习过,望大虾指导
...全文
619 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
corn8888 2012-09-18
  • 打赏
  • 举报
回复
这个问题有点难,时间复杂度
安特矮油 2012-09-18
  • 打赏
  • 举报
回复
84182381
Jay_+wqq_635731323 2012-09-17
  • 打赏
  • 举报
回复
非常完美,不知道该怎么感谢您,稍后结贴,可否留个交流方式,我的635731323[Quote=引用 4 楼 的回复:]

正则表达式替换所有就行了
Java code

public static void main(String[] args) throws Exception {
String s = "SRC : url ( \"ZNJK_Basma.ttf\");";
s = s.replaceAll("((?i)src\\s*:\\s*(?i)url)\\s*\……
[/Quote]
xiars123 2012-09-17
  • 打赏
  • 举报
回复
擦,出bug了,楼主结帖率超过100%!
安特矮油 2012-09-17
  • 打赏
  • 举报
回复
正则表达式替换所有就行了

public static void main(String[] args) throws Exception {
String s = "SRC : url ( \"ZNJK_Basma.ttf\");";
s = s.replaceAll("((?i)src\\s*:\\s*(?i)url)\\s*\\(.+?\\);", "$1(\"myfont.ttf\");");
System.out.println(s);
}
菖蒲老先生 2012-09-17
  • 打赏
  • 举报
回复
你这字符串是存在文件里的,
还是单纯的一个字符串,
文件就逐行读取,循环替换,
单独的就把ls的text换成你自己的不就行了?

[Quote=引用 2 楼 的回复:]

哥们在吗?
你的方法只是匹配了一个,有没有能匹配全文的,我的QQ635731323引用 1 楼 的回复:

Java code
public class Test {
public static void main(String[] args) {
String text = "@font-face {" + "font-family: UKK_Basma;"
+ "font-s……
[/Quote]
Jay_+wqq_635731323 2012-09-17
  • 打赏
  • 举报
回复
哥们在吗?
你的方法只是匹配了一个,有没有能匹配全文的,我的QQ635731323[Quote=引用 1 楼 的回复:]

Java code
public class Test {
public static void main(String[] args) {
String text = "@font-face {" + "font-family: UKK_Basma;"
+ "font-style: normal;" + "font-weight: no……
[/Quote]
Inhibitory 2012-09-17
  • 打赏
  • 举报
回复
public class Test {
public static void main(String[] args) {
String text = "@font-face {" + "font-family: UKK_Basma;"
+ "font-style: normal;" + "font-weight: normal;"
+ "src: url(\"ZNJK_Basma.ttf\");" + "}";

text = text.replaceAll("src: url(.+);", "src: url(\"myfont.ttf\");");

System.out.println(text);

}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧