前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 中正则表达式如何匹配竖线(|) , 以及在 Kotlin 中是如何改进的?

Java 中正则表达式如何匹配竖线(|) , 以及在 Kotlin 中是如何改进的?

作者头像
一个会写诗的程序员
发布2019-07-18 15:23:26
2.3K0
发布2019-07-18 15:23:26
举报
文章被收录于专栏:一个会写诗的程序员的博客

Java 中正则表达式如何匹配竖线(|)

在Java中直接调用String的split方法:

代码语言:javascript
复制
    val b = java.lang.String(a)
    val s3 = b.split("|") // ["a","b","c","|","1","2","3","4"]
    println(JSON.toJSONString(s3))

因为 | 在正则表达式中是或的概念,要想匹配就得用转移字符 "|" 但是 "" 又是java的转移字符,要让其在正则中起作用,就得使用: "\|"

代码语言:javascript
复制
    val b = java.lang.String(a)
    val s3 = b.split("|")
    println(JSON.toJSONString(s3)) // ["a","b","c","|","1","2","3","4"]
    val s4 = b.split("\\|")
    println(JSON.toJSONString(s4)) // ["abc","1234"]

这个Java 中的 split 方法设计简直就是一个"天坑"(天然的坑): 如果不看实现代码,很容易犯错.

代码语言:javascript
复制
public String[] split(String regex) {
        return split(regex, 0);
    }

public String[] split(String regex, int limit) {
        /* fastpath if the regex is a
         (1)one-char String and this character is not one of the
            RegEx's meta characters ".$|()[{^?*+\\", or
         (2)two-char String and the first char is the backslash and
            the second is not the ascii digit or ascii letter.
         */
        char ch = 0;
        if (((regex.value.length == 1 &&
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
             (regex.length() == 2 &&
              regex.charAt(0) == '\\' &&
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
              ((ch-'a')|('z'-ch)) < 0 &&
              ((ch-'A')|('Z'-ch)) < 0)) &&
            (ch < Character.MIN_HIGH_SURROGATE ||
             ch > Character.MAX_LOW_SURROGATE))
        {
            int off = 0;
            int next = 0;
            boolean limited = limit > 0;
            ArrayList<String> list = new ArrayList<>();
            while ((next = indexOf(ch, off)) != -1) {
                if (!limited || list.size() < limit - 1) {
                    list.add(substring(off, next));
                    off = next + 1;
                } else {    // last one
                    //assert (list.size() == limit - 1);
                    list.add(substring(off, value.length));
                    off = value.length;
                    break;
                }
            }
            // If no match was found, return this
            if (off == 0)
                return new String[]{this};

            // Add remaining segment
            if (!limited || list.size() < limit)
                list.add(substring(off, value.length));

            // Construct result
            int resultSize = list.size();
            if (limit == 0) {
                while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
                    resultSize--;
                }
            }
            String[] result = new String[resultSize];
            return list.subList(0, resultSize).toArray(result);
        }
        return Pattern.compile(regex).split(this, limit);
    }

在Kotlin 中, 直接优化了这个 split 方法:

代码语言:javascript
复制
    val a = "abc|1234"

    val s1 = a.split("|")
    val s2 = a.split("\\|")

    println(s1) // [abc, 1234]
    println(s2) // [abc|1234]

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.07.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档