4-30 Java正则匹配

Posted 白天黑夜每日c

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4-30 Java正则匹配相关的知识,希望对你有一定的参考价值。

做CC时经常要用正则表达式过滤数据,当时洗的数据比较复杂,规则比较多。这次做leetcode,复习一下Java的正则匹配。Leetcode 537. Complex Number Multiplication 从表示复数的字符串里把实部和虚部取出来。

http://blog.csdn.net/yin380697242/article/details/52049999

Pattern类,构造函数是私有类型,不能通过new新建,要通过Pattern.compile()获得一个匹配模式。Pattern类可以进行简单的匹配,如字符串的分割,整个字符串的匹配。

Matcher类,通过Pattern.matcher(string)获得Matcher对象。matcher.find()方法,尝试在输入字符串里进行下一次匹配,每做一次匹配后m.start()和m.end()都会改变。

Greedy/Reluctant/Possessive https://docs.oracle.com/javase/tutorial/essential/regex/quant.html

Greedy模式如X? 首先尝试匹配整个字符串,若没找到匹配,则退掉字符串最后一个字符,重新尝试匹配。

Reluctant模式如X?? 首先从字符串头开始匹配,若没找到匹配,每次加一个字符,重新匹配。

Possesive模式如X?+ 尝试匹配整个字符串,若没找到匹配,直接返回结果。

例子:

Enter your regex: .*foo  // greedy quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.

Enter your regex: .*?foo  // reluctant quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfoo" starting at index 0 and ending at index 4.
I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.

Enter your regex: .*+foo // possessive quantifier
Enter input string to search: xfooxxxxxxfoo
No match found.

*, ?, +的区别

*和?都可以匹配0个或多个,存在zero-length匹配,而+匹配时至少存在一个

Enter your regex: a?
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.

Enter your regex: a*
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.

Enter your regex: a+
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.

*和? 的区别

在下面的情况中,对?匹配了多次,而*匹配了最长的一次。

Enter your regex: a?
Enter input string to search: aaaaa
I found the text "a" starting at index 0 and ending at index 1.
I found the text "a" starting at index 1 and ending at index 2.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "a" starting at index 3 and ending at index 4.
I found the text "a" starting at index 4 and ending at index 5.
I found the text "" starting at index 5 and ending at index 5.

Enter your regex: a*
Enter input string to search: aaaaa
I found the text "aaaaa" starting at index 0 and ending at index 5.
I found the text "" starting at index 5 and ending at index 5.

Enter your regex: a+
Enter input string to search: aaaaa
I found the text "aaaaa" starting at index 0 and ending at index 5.

这道Leetcode取实部和虚部的办法:

int[] extractOp(String complex) {
        Pattern p = Pattern.compile("([-0-9]*)\\+([-0-9]*)i");
        Matcher m = p.matcher(complex);
        String tmp;
        int[] re = new int[2];
        while(m.find()) {
            tmp = m.group(1);
            if (tmp.startsWith("-")) {
                re[0] = -(int)Integer.valueOf(tmp.substring(1,tmp.length()));
            } else {
                re[0] = Integer.valueOf(tmp);
            }
            tmp = m.group(2);
            if (tmp.startsWith("-")) {
                re[1] = -(int)Integer.valueOf(tmp.substring(1,tmp.length()));
            } else {
                re[1] = Integer.valueOf(tmp);
            }
        }
        return re;
    }

 

以上是关于4-30 Java正则匹配的主要内容,如果未能解决你的问题,请参考以下文章

正则表达式匹配特定的 URL 片段而不是所有其他 URL 可能性

循环通过 python 正则表达式匹配

java 正则简单使用

通过 Java 正则表达式提取 semver 版本字符串的片段

JAVA正则表达式代码

正则匹配开头为''结尾为','应该怎么写?