1.java中 取一个以"<author"开始,</author>结尾,中间不包含子串“<author”的字符串,用正则表达式如何写?
比方说字符串为:"<author first ><author second>fddsa-authorddddfehauthor</author>".
期待结果为"<author second>fddsa-authorddddfehauthor</author>".
希望大大们帮忙下。谢谢了
比方说字符串为:"<author first ><author second>fddsa-authorddddfehauthor</author>".
期待结果为"<author second>fddsa-authorddddfehauthor</author>".
希望大大们帮忙下。谢谢了
public static void main(String[] args) {
String str="<author first > <author first ><author first ><author first ><author second>fddsa-authorddddfehauthor </author>";
Pattern pattern = Pattern.compile("(<author [^>]*>[^<]+</author>)",Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = pattern.matcher(str);
while(m.find()) {
System.out.println(m.group(1));
}
}
能匹配出“<author second><dd ></author”吗?
//按楼主要求,改过的,
public static void main(String[] args) {
String str="<author first1 > <author first2 ><author first3 ><bbb><aaa>fddsa-authorddddfehauthor </author>";
Pattern pattern = Pattern.compile("(<author [^>]*>(?=<[^author][^>]*>).*?</author>)",Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = pattern.matcher(str);
while(m.find()) {
System.out.println(m.group(1));
}
}
[^author]匹配的是 不包括author字符串呢还是不包括a,u,t,h,o,r任意一个字符??
你说 [^author]匹配的是 不包括author字符串,
实际上不是这样的,匹配的是不包括a,u,t,h,o,r任意一个字符
想匹配a,u,t,h,o,r任意一个字符
用的应该是:String regex = "[^a|u|t|h|o|r]";
想匹配a,u,t,h,o,r任意一个字符,应该是:String regex = "[a|u|t|h|o|r]";
String regex = "[^a|u|t|h|o|r]"; 匹配的是不包括a,u,t,h,o,r任意一个字符
我看了下参考文档,文档上说的是
[^abc] 匹配 "a","b","c" 之外的任意一个字符,而不是abc字符串
http://www.regexlab.com/zh/regref.htm
[xyz] 字符集合。匹配所包含的任意一个字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'。
[^xyz] 负值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'。
我仔细看了一下文档,帮你改了一下,之前写的有问题,public static void main(String[] args) {
String str="<author first1 ><author first2 ><author first3 ><aaa><bbb>fddsa-authorddddfehauthor </author>";
String regex = "(<author[^>]*>(?!<author).*?</author>)";
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = pattern.matcher(str);
while(m.find()) {
System.out.println(m.group(1));
}
}