日期:2014-05-19  浏览次数:20655 次

帮忙改下正则表达式. 谢谢
public static void main(String[] args)
  {

  String str = "http://localhost:8080/sshframework/param/queryParamData.action?vpar=2012-8-17-10-26-16-0.40798535273884656&status=&pageNo=1&dtsNo=&versionNo=&pageSize=10&dateEnd=&dateBegin=&applyPerson=&paramType=&";
  String str1 = "http://localhost:8080/sshframework/param/queryParamData.action?";
  String reg = "/(\\w+)(/\\w+)(\\.action)(\\?vpar)";
  Pattern p = Pattern.compile(reg);
  Matcher mt = p.matcher(str);
  Matcher m = p.matcher(str1);
  String nameSpace = "";
  if (mt.find())
  {
  nameSpace = mt.group(1);
  }
  System.out.println(nameSpace);
   
  if (m.find())
  {
  System.out.println(m.group(1));  
  }
   
  }

上面是 我改进的一个正则表达式,但是达不到要求、

 我需求是:
 当有.action?vpar的时候 不取param这个字符串,并且返回为空字符串。
当只有.action的时候取param 。 并且返回这个字符串。

------解决方案--------------------
Java code

    public static void main(String[] args) {

        String str = "http://localhost:8080/sshframework/param/queryParamData.action?vpar=2012-8-17-10-26-16-0.40798535273884656&status=&pageNo=1&dtsNo=&versionNo=&pageSize=10&dateEnd=&dateBegin=&applyPerson=&paramType=&";
        String str1 = "http://localhost:8080/sshframework/param/queryParamData.action?";
        String reg = "/(\\w+)(/\\w+)(\\.action\\?)$";
        Pattern p = Pattern.compile(reg);
        Matcher mt = p.matcher(str);
        Matcher m = p.matcher(str1);
        String nameSpace = "";
        if (mt.find()) {
            nameSpace = mt.group(1);
        }
        System.out.println(nameSpace);

        if (m.find()) {
            System.out.println(m.group(1));
        }

    }

------解决方案--------------------
探讨

引用:
引用:
public static void main(String[] args)
{

String str = "http://localhost:8080/sshframework/param/queryParamData.action?vpar=2012-8-17-10-26-16-0.40798535273884656&……