验证

简单验证

String regex = "\\d{4}-\\d{2}-\\d{2}";
String input = "2016-01-01";
assertTrue(input.matches(regex));
assertTrue(Pattern.matches(regex, input));

提取

String regex = "\\d{4}-\\d{2}-\\d{2}";
String input = "2016-01-01, 2016-02-02. [2016-03-03]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group());
}

替换

简单替换

String regex = "(\\d{4})-(\\d{2})-(\\d{2})";
String replacement = "$2/$3/$1";
String input = "2016-01-15, 2016-02-15.";
String actual = input.replaceAll(regex, replacement);
String expected = "01/15/2016, 02/15/2016.";
assertEquals(expected, actual);

使用 Pattern 对象,方便反复使用

String regex = "(\\d{4})-(\\d{2})-(\\d{2})";
String replacement = "$2/$3/$1";
String input = "2016-01-15, 2016-02-15.";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
String actual = matcher.replaceAll(replacement);
String expected = "01/15/2016, 02/15/2016.";
assertEquals(expected, actual);

切分

简单切分

String regex = "\\s+";
String input = "a b\tc";
String[] actuals = input.split(regex);
String[] expecteds = new String[] {"a", "b", "c"};
assertArrayEquals(expecteds, actuals);

使用 Pattern 对象,方便反复使用

String regex = "\\s+";
String input = "a b\tc";
Pattern pattern = Pattern.compile(regex);
String[] actuals = pattern.split(input);
String[] expecteds = new String[] {"a", "b", "c"};
assertArrayEquals(expecteds, actuals);

Java - 正则表达式常用操作的更多相关文章

  1. Java Map常用操作

    Java之map常用操作 package basic; import java.util.HashMap; import java.util.Map; /** *Map常用操作方法 */ public ...

  2. Java 正则表达式实例操作

    Regular Expression正则表达式,简称RegExp,常规通用的表达式,在多个开发语言中都有它的实现,可以通过正则表达式来快速的检索.匹配.查找.替换字符串中的文本. 简单实例 匹配网址 ...

  3. Java 线程常用操作

    继Java线程生命周期继续学习Java线程其他常用操作 线程的常用操作 设置线程名字:setName() 获取线程名称:getName() 线程唯一Id:getId() // 自定义线程名称 Stri ...

  4. Java File 常用操作回顾

    最近项目中要用到File这个类,温故而知新,回过头来回顾下这个File类,File类主要是对磁盘目录,文件进行操作的Api,具体其实查JDK api 的File全能获取到. 下面写一些文件目录的基本操 ...

  5. HDFS Java API 常用操作

    package com.luogankun.hadoop.hdfs.api; import java.io.BufferedInputStream; import java.io.File; impo ...

  6. java正则表达式常用实例——借鉴思路

    转载自:http://mp.weixin.qq.com/s?__biz=MjM5OTM4NDMyMg==&mid=2650044497&idx=1&sn=dc80fa35f7e ...

  7. java集合常用操作

    收集一些常用集合操作的代码,用于治疗健忘症,:) set转list //构造Map数据 Map<String, String> map = new HashMap<String, S ...

  8. 【转】Java 字符串常用操作(String类)

    原文网址:http://www.cnblogs.com/freeabyss/archive/2013/05/15/3187057.html 字符串查找 String提供了两种查找字符串的方法,即ind ...

  9. Java 字符串常用操作(String类)

    字符串查找 String提供了两种查找字符串的方法,即indexOf与lastIndexOf方法. 1.indexOf(String s) 该方法用于返回参数字符串s在指定字符串中首次出现的索引位置, ...

随机推荐

  1. HDU 5266 pog loves szh III (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma ...

  2. Egret项目Typescript的编译报错

    今天编译项目,出现了一个奇怪的报错,如下: E:\engine\egret-core-3.1.2\tools\lib\typescript\tsclark.js:41531 1> if (fil ...

  3. Lua学习笔记(六):协程

    多线程和协程 多线程是抢占式多任务(preemptive multitasking),每个子线程由操作系统来决定何时执行,由于执行时间不可预知所以多线程需要使用同步技术来避免某些问题.在单核计算机中, ...

  4. 定义文档兼容性,让IE按指定的版本解析我们的页面

    作为开发人员,特别是作为Web的前端开发人员 ,最悲催的莫过于要不断的,不断的去调试各种浏览器的显示效果,而这其中最让人头痛的莫过于MS下的IE系列浏览器,在IE系列中的调试我们将会发现没有一个是好伺 ...

  5. MFC拆分窗口及它们之间的数据交换(转)

    转自:http://blog.csdn.net/nuptboyzhb/article/details/7455471 源代码:http://download.csdn.net/detail/nuptb ...

  6. Display:Block

    根据CSS规范的规定,每一个网页元素都有一个display属性,用于确定该元素的类型,每一个元素都有默认的display属性值,比如div元素,它的默认display属性值为“block”,成为“块级 ...

  7. ibatis 搭建总结

    一.搭建ibatis环境 1.导入ibatis的jar包,已及数据库驱动jar包ibatis-2.3.0.677.jar ibatis-dao-2.jar ibatis-sqlmap-2.jar ib ...

  8. TCP四种定时器--学习笔记

    TCP使用四种定时器: 重传定时器(Retransmission Timer).坚持定时器(Persistent Timer).保活定时器(Keeplive Timer).时间等待定时器(Time_W ...

  9. WPF基础到企业应用系列7——深入剖析依赖属性(WPF/Silverlight核心)

    一. 摘要 首先圣殿骑士非常高兴这个系列能得到大家的关注和支持.这个系列从七月份開始到如今才第七篇,上一篇公布是在8月2日,掐指一算有二十多天没有继续更新了,最主要原因一来是想把它写好,二来是由于近期 ...

  10. delphi SpeedButtonDown

    的属性 的事件 的方法   设置SpeedButton的Down的属性      AllowAllUp属性 当有多个SpeedButton时 让有2个按钮都能处于按下状态 设置它的GroupIndex ...