码迷,mamicode.com
首页 > 编程语言 > 详细

4-30 Java正则匹配

时间:2017-04-30 15:10:25      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:ora   article   text   简单   tar   integer   tutorial   new   类型   

做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正则匹配

标签:ora   article   text   简单   tar   integer   tutorial   new   类型   

原文地址:http://www.cnblogs.com/yuchenkit/p/6789435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!