正则表达式
来源:互联网 发布:广联达软件怎样下载 编辑:程序博客网 时间:2024/05/08 09:55
public class RegexTest { public static void main(String[] args) {// replaceFirst replaceAll split matches String string = "hello, ironman ~, 233"; System.out.println(string.replaceFirst("\\w*", "丶"));//数量表示符默认采用贪婪模式,除非另有表示。 (最大匹配)// 因为"\\w*"采用了贪婪模式,数量表示符*会一直匹配下去,所以该字符串前面所有单词都会被他匹配到,直到遇到逗号// 除了默认的贪婪模式外还有勉强模式?和占有模式+ System.out.println(string.replaceFirst("\\w*?", "丶"));//relucant 勉强模式 ,匹配最少的字符 (最少匹配) System.out.println(string.replaceFirst("\\w*+,", "丶"));//Possessive 占有模式 (完全匹配) System.out.println(string.replaceAll("\\d+?", "x")); String[] strings = string.split("\\s+"); for (String string1 : strings) { System.out.println(string1); } System.out.println(string.matches("\\w+, \\w+ ~, \\d+"));//事实上String类中同样提供了matches方法 ,该方法返回该字符串是否匹配指定的正则表达式// Pattern and Matcher//1 string = "EEEch0~"; Pattern pattern = Pattern.compile("E+ch0~"); Matcher matcher = pattern.matcher(string); System.out.println(matcher.matches());//true//2 System.out.println(Pattern.matches("E+ch0~", string));// 如果某个正则表达式仅需使用一次,则可以直接使用pattern类的静态matches方法 matcher.reset("this is a new string.");//将现有的matcher对象用于新的字符串 System.out.println(matcher.matches());//false //find group start end lonkingAt string = " just do it ! no one match you! "; System.out.println("targetString :" + string); matcher = Pattern.compile(" \\w+").matcher(string); while (matcher.find()) { //其中 find 还可以带一个int参数,带参数的将从该int索引处向下搜索 System.out.println(matcher.group() + "\t start index:" + matcher.start() + "\t end index:" + matcher.end()); }//group返回上一次与pattern匹配的子串,start (end) 返回上次与pattern匹配的子串在目标字符串中的起始位置,(结束位置+1) System.out.println(matcher.lookingAt());//lonkingAt返回目标字符串前面部分与pattern是否匹配 }}
0 0
- 【正则表达式】正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- oracle账号未解锁情况
- Jenkins-Git trouble shooting
- java中nextLine()和next()的区别
- 怎么运用EDIUS中的打字效果
- trim去除空格
- 正则表达式
- 什么是同步以太网
- 从MongoDb读文件
- IIS 8.0~10.0 详细错误 - HTTP 错误 401.2 - Unauthorized 解决方法
- 07-贝叶斯曲线拟合
- 困难的A+B
- Intellj IDEA 快捷键
- java 中使用shutdownhook 清理资源
- 计算机图形学学习笔记