比如:
<File.Author>Adi Harn</File.Author>
第一关键字:<File.Author>
第二关键字:</File.Author>可以有多个author,所以希望正则表达式匹配多次,而不希望用substring循环解析谢谢
<File.Author>Adi Harn</File.Author>
第一关键字:<File.Author>
第二关键字:</File.Author>可以有多个author,所以希望正则表达式匹配多次,而不希望用substring循环解析谢谢
...substring(str.indexof(key1)+key1.length(), str.indexof(key2))
}
不就是substring 循环解析么?
......String regExp="author(\w*)author"
String inputText="author1234authorauthor123author'
......
调用类似下面的函数public static void regularExpression(String regExp, String inputText) {
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(inputText); System.out.println("Regular Expression: " + regExp);
System.out.println("Input text: " + inputText);
System.out.println(); while (matcher.find()) { System.out.println(matcher.group()); System.out.println("matcher.groupCount:" + matcher.groupCount()); for (int i = 0; i < matcher.groupCount() + 1; i++) { System.out.println("matcher.group(" + i + "):"
+ matcher.group(i)); }
System.out.println();
}
}你把上面的代码试试就知道了
import java.util.regex.Pattern;public class Test { public static void main(String[] args) {
String input = "<File.Author>Adi Harn </File.Author> " +
"<File.Author> TestAuthor </File.Author>";
Matcher m = Pattern.compile("(?is)<([\\w\\.\\-:]+)>\\s*([^<>]+?)\\s*</\\1>").matcher(input);
while (m.find()) {
String s = m.group(2);
System.out.println(s);
}
}}
String[] strs = str.split("(</File.Author>.*<File.Author>)|(</File\\.Author>.*$)");
strs[0] = strs[0].replaceFirst(".*<File.Author>", "");strs字符串数组就是里面的类容了喵~```
for... {
...substring(str.indexof(key1)+key1.length(), str.indexof(key2))
}