zoukankan      html  css  js  c++  java
  • 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;
        }
    
  • 相关阅读:
    python之天气爬虫
    python之一异常问题(TypeError: object of type 'NoneType' has no len())
    数据分析之漏斗分析
    python之pytest_addoption : 命令行参数
    python之一driver.find_element_by_xpath与driver.find_element(by, value)的区别
    python之正则表达式从列表中取值报类型错误
    python之append和extend的区别
    pyton之字典的使用
    python之pd.DataFrame函数使用
    python之正则表达式 re.findall 用法
  • 原文地址:https://www.cnblogs.com/yuchenkit/p/6789435.html
Copyright © 2011-2022 走看看