24tb_LoginNameOrLoginEmail=work_test&ctl00
我有一段内容,想截取LoginNameOrLoginEmail=work_test想用正则表示
但正则中&为特殊字符
我的正则是这样写得  LoginNameOrLoginEmail=.*[^&]
但这样就截到句子末尾了
请问下高手们 & 正则怎么表示
/&好像不大管用

解决方案 »

  1.   

    ^ 表示开头
    $ 表示结尾
    public void test01() throws Exception {
    String s = "24tb_LoginNameOrLoginEmail=work_test&ctl00";
    Pattern p = Pattern.compile("^(.*)_LoginNameOrLoginEmail=(.*)$");
    Matcher m = p.matcher(s);
    if (m.matches()) {
    System.out.println("Match");
    System.out.println(m.groupCount());
    // System.out.println(m.group(0));
    System.out.println(m.group(1));
    System.out.println(m.group(2));
    }
    }
      

  2.   

    public void test01() throws Exception {
            String s = "24tb_LoginNameOrLoginEmail=work_test&ctl00";
            Pattern p = Pattern.compile("^(.*)_LoginNameOrLoginEmail=(.*)$");
            Matcher m = p.matcher(s);
            if (m.matches()) {
                System.out.println("Match");
                System.out.println(m.groupCount());
    //            System.out.println(m.group(0));
                System.out.println(m.group(1));        
                System.out.println(m.group(2));