当前位置: 代码迷 >> J2SE >> java 正则表达式获取值,该如何处理
  详细解决方案

java 正则表达式获取值,该如何处理

热度:86   发布时间:2016-04-24 12:19:15.0
java 正则表达式获取值
比如以下有规律的文件,如何通过正则表达式获取path的值
<classPath>
<classpathentry kind="lib" path="/bss.lib/third-lib/jakarta-commons/commons-beanutils-1.7.0.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/jakarta-commons/commons-codec-1.3.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/jakarta-commons/commons-collections-3.2.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/jakarta-commons/commons-lang-2.4.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/dom4j/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/dom4j/jaxen-1.1.1.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/spring/spring-2.5.6.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/j2ee/servlet-api.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/jakarta-commons/commons-httpclient-3.0.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/log4j/log4j-1.2.14.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/logback/logback-classic-0.9.16.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/logback/logback-core-0.9.16.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/logback/slf4j-api-1.5.8.jar"/>
<classpathentry kind="lib" path="/bss.lib/third-lib/jakarta-commons/commons-logging-1.1.1.jar"/>
</classPath>

我写了段正则,可能正则写的有问题,没有找到匹配的path, 我的正则path=[\\w]*$/,认为是以path=开头,到/结尾的就找到匹配的了,但是结果出不来,请高手斧正,并指出原因  
Pattern pattern = Pattern
.compile("path=[\\w]*$/");
Matcher matcher = pattern.matcher(xml);
if (matcher.find()) {
serviceCode = matcher.group();
}
System.out.println(serviceCode);

------解决方案--------------------
Java code
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));        }    }}
  相关解决方案