ambitiouswolf 2013-04-27 19:04
浏览 201
已采纳

正则表达式问题 ' " \

代码如下:
[code="java"]

System.out.println("\'");
System.out.println("\'");
System.out.println("\\'");
System.out.println("\\"");
String a = "'\"";
System.out.println("1" + a.replace("'", "\'"));
System.out.println("2" + a.replace("'", "\'"));
System.out.println("3" + a.replace("'", "\\'"));
System.out.println("4" + a.replace("'", "\\'"));
System.out.println("5" + a.replaceAll("\"", "\\""));
System.out.println("6" + a.replaceAll("\"", "\\\""));

[/code]

运行结果:
[code="java"]

'
\'
\'
\"
1'"
2\'"
3\'"
4\'"
5'"
6'\"

[/code]

问题:
3中第二个参数字面值为\',结果输出为\';为啥5中第二个参数字面值为\",结果输出不是\"而是"?怎么想都想不明白啊。

  • 写回答

4条回答 默认 最新

  • jinnianshilongnian 2013-04-27 20:15
    关注

    1、 ' 和 \' --都输出--> ' 也就是写不写\都一样

    2、
    "3" + a.replace("'", "\\'")
    3\\'\" ---> 3\'" 即\ -->\ \'--->' \"--->"

    3、replaceAll是正则表达式替换
    "5" + a.replaceAll("\"", "\\"")


    [code="java"]public String replaceAll(String regex, String replacement) {
    return Pattern.compile(regex).matcher(this).replaceAll(replacement);
    }[/code]
    ---------->
    [code="java"]public String replaceAll(String replacement) {
    reset();
    boolean result = find();
    if (result) {
    StringBuffer sb = new StringBuffer();
    do {
    appendReplacement(sb, replacement);
    result = find();
    } while (result);
    appendTail(sb);
    return sb.toString();
    }
    return text.toString();
    }
    [/code]
    重点是
    public Matcher appendReplacement(StringBuffer sb, String replacement) {

        // If no match, return error
        if (first < 0)
            throw new IllegalStateException("No match available");
    
        // Process substitution string to replace group references with groups
        int cursor = 0;
        StringBuilder result = new StringBuilder();
    
        while (cursor < replacement.length()) {
            char nextChar = replacement.charAt(cursor);
           [color=red] if (nextChar == '\\') {
                cursor++;
                nextChar = replacement.charAt(cursor);
                result.append(nextChar);[/color]
                cursor++;
            } else if (nextChar == '$') {
                // Skip past $
                cursor++;
                // A StringIndexOutOfBoundsException is thrown if
                // this "$" is the last character in replacement
                // string in current implementation, a IAE might be
                // more appropriate.
                nextChar = replacement.charAt(cursor);
                int refNum = -1;
                if (nextChar == '{') {
                    cursor++;
                    StringBuilder gsb = new StringBuilder();
                    while (cursor < replacement.length()) {
                        nextChar = replacement.charAt(cursor);
                        if (ASCII.isLower(nextChar) ||
                            ASCII.isUpper(nextChar) ||
                            ASCII.isDigit(nextChar)) {
                            gsb.append(nextChar);
                            cursor++;
                        } else {
                            break;
                        }
    

    此处红色部分 即如果匹配\ 那么跳过,则
    5'\\" ---> 5'\"--得到-->5'"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题