会java 正则都可以看看这个问题

岁月之梦 2010-12-16 04:17:29
java字符串如下:
String s = "<node id=\"04\" text=\"CEC\"><node id=\"0401\" text=\"npar1\">test1</node><node id=\"0402\" text=\"npar1\">test2</node></node>";

“\” 为java对"的转换。

现在就是想要这样一个结果
我传 04 的时候 返回 <node id=\"0401\" text=\"npar1\">test1</node><node id=\"0402\" text=\"npar1\">test2</node>


我传 0401 的时候 返回 test1

以此类推 我给的 id 不同 返回的只是<node id="id值"></node> 里面的内容。

我现在匹配对了04 的时候 0401就匹配不对了,匹配对了0401,04的时候又不对了!

恳请懂正则的高手 指点指点。
...全文
71 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
岁月之梦 2010-12-16
  • 打赏
  • 举报
回复
e9876 强!结贴了!
tubage408 2010-12-16
  • 打赏
  • 举报
回复
while(m.find()){
System.out.println(m.group(1));
}
tubage408 2010-12-16
  • 打赏
  • 举报
回复
试试这个可以不
String id = "0402";
String s = "<node id=\"04\" text=\"CEC\">" +
"<node id=\"0401\" text=\"npar1\">test1</node>" +
"<node id=\"0402\" text=\"npar1\">test2</node>" +
"</node>";
Pattern p = Pattern.compile("<node id=\""+id+"\" text=\"\\w+\">(\\w+|.+)</node>");
Matcher m = p.matcher(s);
dr_lou 2010-12-16
  • 打赏
  • 举报
回复
xml变成map
e9876 2010-12-16
  • 打赏
  • 举报
回复

String s = "<node id=\"04\" text=\"CEC\"><node id=\"0401\" text=\"npar1\">test1</node><node id=\"0402\" text=\"npar1\">test2</node></node>";

String number = "0401";

int index =0;

int start =0;

int end = 0;

Stack<String> stack = new Stack<String>();

String regex1 = "<(\\w+) [^\\>]*\""+number+"\"[^\\>]*>";

String regex2 = "<(/?\\w+)[^\\>]*>";

Pattern p = Pattern.compile(regex1);

Matcher m = p.matcher(s);

if(m.find()){
index = m.end();
start = m.end();

stack.push(m.group(1));

p = Pattern.compile(regex2);
m=p.matcher(s);

String matchStr = null;

while(stack.size()!=0&&m.find(index)){
matchStr = m.group(1);
// System.out.println(matchStr);
if(matchStr.startsWith("/")){
if(matchStr.equals("/"+stack.peek())){
stack.pop();
}
}else{
stack.push(matchStr);
}
index= m.end();
}
if(stack.size()!=0){
throw new Exception("总之是出错了");
}
end = m.start();

System.out.println("结果为:"+s.substring(start,end));

}else{
throw new Exception("没有找到ID为"+number+"的节点");
}


真麻烦。。。LZ拿去试试吧。
Jlins 2010-12-16
  • 打赏
  • 举报
回复
呵呵 这就比较麻烦了 ...[Quote=引用 5 楼 yingkeda 的回复:]

现在xml 已经有了!总不能改变人家给的格式吧!
[/Quote]
岁月之梦 2010-12-16
  • 打赏
  • 举报
回复
现在xml 已经有了!总不能改变人家给的格式吧!
e9876 2010-12-16
  • 打赏
  • 举报
回复
这样的话。。
写进一个空的xml文件里。然后解析XML会比较好。。
岁月之梦 2010-12-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 e9876 的回复:]

Java code

String s = "<node id=\"04\" text=\"CEC\"><node id=\"0401\" text=\"npar1\">test1</node><node id=\"0402\" text=\"npar1\">test2</node></node>";

String number = "04";
……
[/Quote]

不是的,是这样
String s = "<node id="04" text="CEC"><node id="0401" text="npar1">test1</node><node id="0402" text="npar1">test2</node></node>"


给04 返回 <node id="0401" text="npar1">test1</node><node id="0402" text="npar1">test2</node>

给0401 返回test

假如字符串 s = “<node id="04" text="CEC"><node id="0401" text="npar1"><node id="040101" text="test3">test3</node></node><node id="0402" text="npar1">test2</node></node>”

那么给0401 就应该返回 <node id="040101" text="test3">test3</node>
e9876 2010-12-16
  • 打赏
  • 举报
回复

String s = "<node id=\"04\" text=\"CEC\"><node id=\"0401\" text=\"npar1\">test1</node><node id=\"0402\" text=\"npar1\">test2</node></node>";

String number = "04";

String regex = "<(\\w+) [^\\>]*"+number+"[^\\>]*>[^\\<]*</\\1>";

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
int index = 0;
while(m.find(index)){
System.out.println(m.group());
index = m.end();
}

。。。
e9876 2010-12-16
  • 打赏
  • 举报
回复

String s = "<node id=\"04\" text=\"CEC\"><node id=\"0401\" text=\"npar1\">test1</node><node id=\"0402\" text=\"npar1\">test2</node></node>";

String number = "04";

String regex = "<[^\\>]*"+number+"[^\\>]*>";

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
int index = 0;
while(m.find(index)){
System.out.println(m.group());
index = m.end();
}

这样吗。

50,504

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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