博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
4-30 Java正则匹配
阅读量:7000 次
发布时间:2019-06-27

本文共 3185 字,大约阅读时间需要 10 分钟。

做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 quantifierEnter input string to search: xfooxxxxxxfooI found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.Enter your regex: .*?foo  // reluctant quantifierEnter input string to search: xfooxxxxxxfooI 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 quantifierEnter input string to search: xfooxxxxxxfooNo match found.

*, ?, +的区别

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

Enter your regex: a?Enter input string to search: aI 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: aI 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: aI found the text "a" starting at index 0 and ending at index 1.

*和? 的区别

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

Enter your regex: a?Enter input string to search: aaaaaI 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: aaaaaI 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: aaaaaI 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;    }

 

转载于:https://www.cnblogs.com/yuchenkit/p/6789435.html

你可能感兴趣的文章
Ubuntu16.04交叉工具链安装
查看>>
我的友情链接
查看>>
CentOS下Denyhosts的安装和使用
查看>>
ORA-01722:invalid number 解决方法
查看>>
关于android中shape的使用
查看>>
Java Web开发初级篇之常见异常处理
查看>>
【Sensors】位置传感器(4)
查看>>
spring mvc中log4j的配置与使用
查看>>
Linux基本命令操作练习
查看>>
gridview多标题和后台生成标题控件
查看>>
linux 命令之 --用户管理
查看>>
Flask + Gunicorn + Nginx 部署
查看>>
二叉树基本操作实现
查看>>
linux 第三周作业
查看>>
App Launch time 101 (Android Performance Patterns Season 6 Ep. 1)
查看>>
回首大学过往的时光所感
查看>>
IT生活
查看>>
Linux配置开机启动项
查看>>
socket编程(一)
查看>>
javascript获取select的值
查看>>