求助java正则表达式问题,我现在想用java程序操作我本地的a.css文件,并且对此文件里边的代码做更改。

例如我a.css文件里边的代码为:body{background:nonerepeatscroll00#FFFFFF;font-size:12px;line-height... 例如我a.css文件里边的代码为:
body {
background: none repeat scroll 0 0 #FFFFFF;
font-size: 12px;
line-height: 160%;
text-align: center;
}
hr {
margin: 0;
padding: 0;
}
.......
这是我的java方法:
public static String utilUpdate(String fileName,String selectorName,String content)
里边的三个参数分别是:css文件名称、选择器名称(例如body)、更改内容(例如:background: none repeat scroll 0 0 #FFFFFF;)
现在假如说我要改 选择器body下的background属性内容怎么改啊?

我写的是一个公用的更改css样式属性的方法,有人跟我用正则表达式写比较简单,还说变成什么一个对象key、value形式的。。。我蒙了、求大神帮忙啊,能帮我成功解决的,必定重谢。。。。跪求代码。。。
展开
 我来答
创哥很忙
2013-04-03
知道答主
回答量:34
采纳率:0%
帮助的人:20.7万
展开全部
既然你只是问了正则表达式那文件读取肯定知道吧。
读取每一行 str .通过匹配 str.matches("\s*background:.*)==true 就找到了background: none repeat scroll 0 0 #FFFFFF; 这一行
\s* 前面是任意多个空格
background:

.* 任意个字符
追问
我是想找body下的background  。按你说的循环每一行,那我假如下边的hr下也有个background呢?那岂不是顺序乱了 ?还是不能准确定位啊。我这是想改哪个就改哪个,不是固定的。。
追答
说再多没用  按你的需求 我给你写一个
public class ChangeFileContent {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "body{\n"+
"background: #ffffff;\n"+
"font-size;\n"+
"}\n"+
"hr{\n"+
"}";
//System.out.println(str);
changeContent(str,"body","background","helloworld");
}

//str 读取的文本拼成一个字符串blockType 块名称 attribute 属性名称 content 想要修改的文本
public static void changeContent(String str,String blockType,String attribute,String content){
String blockStr = str.substring(str.indexOf(blockType+"{"),1+str.indexOf("}", str.indexOf(blockType+"{")));
//System.out.println(blockStr);
String attrStr = blockStr.substring(blockStr.indexOf(attribute), 1+blockStr.indexOf(";",blockStr.indexOf(attribute)));
//System.out.println(attrStr);
String oldAttr = attrStr.substring(attrStr.indexOf(":")+1,attrStr.indexOf( ";"));
//System.out.println(oldAttr);
String newAttrStr = attrStr.replace(oldAttr, content);
//System.out.println(newAttrStr);
str = str.replace(attrStr, newAttrStr);
System.out.println(str);
}
}
嘉窈诺雪
2013-04-03 · TA获得超过168个赞
知道答主
回答量:82
采纳率:0%
帮助的人:14.7万
展开全部
String test = "body { background: none repeat scroll 0 0 #FFFFFF; "
+ " font-size: 12px; line-height: 160%; "
+ " text-align: center;}" + "hr { margin: 0; "
+ " padding: 0;}";

Map<String,HashMap<String,String>> mapClass = new HashMap<String,HashMap<String,String>>();
HashMap<String,String> mapBody = new HashMap<String,String>();
HashMap<String,String> mapHr = new HashMap<String,String>();

mapBody.put("background:","none repeat scroll 0 0 #FFFFFF;");
mapBody.put("font-size:","12px;");
mapBody.put("line-height:","160%;");
mapBody.put("text-align:","center;");
mapHr.put("margin:","0;");
mapHr.put("padding:","0;");

mapClass.put("body",mapBody);
mapClass.put("hr",mapHr);

这样可以根据key值替换掉value值,然后自己写一个方法把它们拼起来
追问
首先谢谢你辛苦写了这么多、但是我感觉这个方法太死了啊,都是手动存进去map的,我的css文件不知道有body和hr啊,有很多... 里边的选择器个数不是固定的,我不能挨个存啊。你明白我的意思?
追答
那我就讲思路了……
首先用正则匹配{....}用split,分割,把分割后的存在数组里面
然后取出array[0],array[2],array[4],....mapCalss的主key=arrayFirst再取出array[1],array[3],array[5],....{...}里面的用split(\\;)
分割得到的array1[0],array1[2],array1[4],....作为主键array1[1],array1[3],array1[5],....
然后得到一个Map mapname里面作为刚在key=arrayFirst 的value
依次下去,然后就存到
Map> mapClass = new HashMap>();

这个map里面去了,然后就可以找key了……
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式