正则表达式在Java中应用的三种典型场合:验证,查找和替换
正则式在编程中常用,总结在此以备考:
package regularexp; import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegUsages {
// 验证例子
private static boolean verify(String code) {
final String patternStr = "\\d+";
return Pattern.matches(patternStr, code);
}
// 查找例子
private static void search() {
Pattern pattern = Pattern.compile("m(o+)n", Pattern.CASE_INSENSITIVE); Matcher matcher=pattern.matcher("Sun Earth moon mooon Mon mooooon Mooon Mars");
while(matcher.find()) {
System.out.println(String.format("%s (%d~%d)", matcher.group(0),matcher.start(),matcher.end()));
}
}
// 更替例子1
private static void replace1() {
String str = "10元 1000人民币 10000元 100000RMB";
str = str.replaceAll("(\\d+)(元|人民币|RMB)", "$1圆");
System.out.println(str);
}
// 更替例子2
private static void replace2() {
Pattern p = Pattern.compile("m(o+)n", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher("Sun Earth moon mooon Mon mooooon Mooon Mars");
StringBuffer sb = new StringBuffer(); boolean result = m.find(); while (result) {
m.appendReplacement(sb, "Moon");
result = m.find();
}
m.appendTail(sb); System.out.println("Result=" + sb.toString());
} public static void main(String[] args) {
System.out.println(String.format("601857 %s a stock code.",verify("601857")?"is":"isn't"));
search();
replace1();
replace2();
}
}
输出:
601857 is a stock code.
moon (10~14)
mooon (15~20)
Mon (21~24)
mooooon (25~32)
Mooon (33~38)
10圆 1000圆 10000圆 100000圆
Result=Sun Earth Moon Moon Moon Moon Moon Mars
--2020-03-06--
正则表达式在Java中应用的三种典型场合:验证,查找和替换的更多相关文章
- java中 this 的三种用法
Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...
- Java中对象的三种状态
Java中的对象的三种状态是和垃圾回收紧密相关的,因此有必要深究. 状态一:可触及态:从根节点开始,可以搜索到这个对象,也就是可以访问到这个对象,也有人将其称为可达状态. 状态二:可复活态:从根节点开 ...
- java中线程的三种实现方式
一下记录下线程的3中实现方式:Thread,Runnable,Callable 不需要返回值时,建议使用Runnable:有返回值时建议使用Callable 代码如下所示: package com.f ...
- Java中Map的三种遍历方法
Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历. 告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...
- Java中Singleton的三种实现方式解析
一.什么是Singleton? <设计模式>的作者.Eclipse和 Junit 的开发者 Erich Gamma 在它的理论体系中将 Singleton 定义为仅仅被实例化一次的类.在当 ...
- Java中配置文件的三种配置位置及读取方式
XML 和properties properties: 1.存放于src根目录下 //获取到同包下的资源文件,将其转换成流对象 //InputStream is= PropertiesDemo.cla ...
- JAVA中this的三种用法的详解
this关键字必须放在非静态方法里面 this关键字代表自身,在程序中主要的使用用途有以下几个方面: ? 使用this关键字引用成员变量 ? 使用this关键字在自身构造方法内部引用其它构造方法 ? ...
- 正则表达式在Java中的使用
目录 介绍 从简单例子认识正则表达式匹配 Java中对正则表达式的支持(各种语言有相应的实现) 初步认识 . + * ? 范围 认识\s \w \d - 下面介绍数字和字母的正则表达, 这是编程中使用 ...
- java 获取随机数的三种方法
方法1(数据类型)(最小值+Math.random()*(最大值-最小值+1))例:(int)(1+Math.random()*(10-1+1))从1到10的int型随数 方法2获得随机数for (i ...
随机推荐
- 微信公众号添加zip
微信公众号添加zip的教程 我们都知道创建一个微信公众号,在公众号中发布一些文章是非常简单的,但公众号添加附件下载的功能却被限制,如今可以使用小程序“微附件”进行在公众号中添加附件. 以下是公众号添加 ...
- ngnix.conf的配置结构
1.ngnix.conf的配置结构 2.部分配置文件说明 #worker进程可操作的用户 #user nobody; #设置worker的个数 worker_processes 1; #错误日志 #e ...
- C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3903 访问. 给定一个已按照升序排列 的有序数组,找到两个数使得 ...
- golang实现 快速排序算法
快速排序算法原理: b站https://b23.tv/uJqRYN package main import "fmt" //[]int{1,2,3,4,5,6,7,8} func ...
- 01 树莓派4B—C语言编程——GPIO
#include <stdio.h>#include <wiringPi.h> int main( void){ int LED1 = 1; int LED4 = 4; wir ...
- Flask实现RESTful API(注意参数位置解析)
准备工作 首先安装flask_restful三方组件 pip install flask_restful 在models.py中新建一个类,生成表,往里面插入一些数据.(flask要想使用ORM的话需 ...
- DataGrid添加进度条列
DataGridColumn类型的继承树 DataGridColumn的派生类: 一般情况下DataGridBoundColumn和DataGridComboBoxColumn足以满足多数列的样式,如 ...
- 用java编程在10000到99999中的数字中找到aabcc类型的数字
package com.diyo.offer; public class FindNum { public static void main(String[] args) { int count = ...
- window下命令启动/停止nginx
查看Nginx的版本号:nginx -v 启动Nginx:start nginx 快速停止或关闭Nginx:nginx -s stop 正常停止或关闭Nginx:nginx -s quit 配置文件修 ...
- Java异步CompletableFuture的使用
所谓异步调用其实就是实现一个可无需等待被调用函数的返回值而让操作继续运行的方法.Java中的CompletableFuture 提供了四个静态方法来创建一个异步操作. public static Co ...