正则表达式中把"\\"替换为"/"怎么换呢?
请教!!!非常感谢

解决方案 »

  1.   

    str.replace("\\", "/")
    这样可以
    为什么要用正则表达式
      

  2.   

    兄弟你搞反了吧,str.replace("/", "\\");,这样吧,
    这样不行,替换不过来的!!!!!!!!!!
      

  3.   

    str.replace("\\\\", "/") = =..而且要这样写。。两个杠代表一个杠。。4个杠代表两个杠
      

  4.   


     = =。。你自己去看 源码吧。。他的参数是String 类型的。。如果是char类型才不是在用正则
      

  5.   

    对!
    str.replace("\\", "/")!!!
    只能转成字符数组形式,再取每一个值进行改变了,
      

  6.   

    源码就不用了,建议你看一下replace和replaceAll
      

  7.   

    String a = "fsdf\\aaa";
    a=a.replaceAll("\\\\", "/");
      

  8.   

    System.out.println("abcdef\\".replace('\\','/'));
    System.out.println("abcdef\\".replace("\\","/"));
      

  9.   

    String s = "111\\1111\\";
    s = s.replace("\\", "/");
    System.out.println(s);
    111/1111/
      

  10.   

    /**
         * Replaces each substring of this string that matches the literal target
         * sequence with the specified literal replacement sequence. The
         * replacement proceeds from the beginning of the string to the end, for
         * example, replacing "aa" with "b" in the string "aaa" will result in
         * "ba" rather than "ab".
         *
         * @param  target The sequence of char values to be replaced
         * @param  replacement The replacement sequence of char values
         * @return  The resulting string
         * @throws NullPointerException if <code>target</code> or
         *         <code>replacement</code> is <code>null</code>.
         * @since 1.5
         */
        public String replace(CharSequence target, CharSequence replacement) {
            return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
                this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
        }
      

  11.   

    呵呵,所有的String都可以看成正则,
    String str = "a" 可以匹配单个"a",相当于[a]{1}
    String str = "a*" 可以匹配多个"a",相当于[a]{1,n}
    当String str = "a*" 匹配多个"a*" 时,这还算一个正则表达式?我上面说的,replace用的不是正则,我指它的参数不是一个正则表达式,楼上的代码中
    Pattern.compile(target.toString(), Pattern.LITERAL)
    这个参数相信你也注意到了,我就不贴上来了。
      

  12.   


    不是一个正则表达式你认为是什么?
    Pattern.LITERAL 是什么意思?设置之后难道就不是一个正则表达式了?那我到得听一下大侠的高见了。
      

  13.   

    当String str = "a*" 匹配多个"a*" 时,这还算一个正则表达式?只不过是因为启用了,LITERAL不会对*看作是一个元字符。当作普通的*来表示。
    好比:String str = "a\*";难道这不是一个表达式吗?
      

  14.   

    我想你错了。 String a = "aa\\".replace("\\", "/");
    System.out.println(a);// aa/
    String b = "aa\\".replaceAll("\\\\", "/");
    System.out.println(b);// aa/