日期:2014-05-20 浏览次数:20698 次
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test1 { public static void main(String[] args) { String str = "<classPath>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/jakarta-commons/commons-beanutils-1.7.0.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/jakarta-commons/commons-codec-1.3.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/jakarta-commons/commons-collections-3.2.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/jakarta-commons/commons-lang-2.4.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/dom4j/dom4j-1.6.1.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/dom4j/jaxen-1.1.1.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/spring/spring-2.5.6.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/j2ee/servlet-api.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/jakarta-commons/commons-httpclient-3.0.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/log4j/log4j-1.2.14.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/logback/logback-classic-0.9.16.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/logback/logback-core-0.9.16.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/logback/slf4j-api-1.5.8.jar\"/>\n" + "<classpathentry kind=\"lib\" path=\"/bss.lib/third-lib/jakarta-commons/commons-logging-1.1.1.jar\"/>\n" + "</classPath>"; // String regex = "(path=\"\\S+\")"; String regex = "path=\"(\\S+)\""; Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); Matcher matcher = pattern.matcher(str); while(matcher.find()) { System.out.println(matcher.group(1)); } } }