使用“.+?”实现中间模糊匹配的代码: public class Test { public static void main(String[] args) { String str="总会在某一个回眸的时刻醉了流年,濡湿了柔软的心.总会有某一个回眸的时刻醉了流年,濡湿了柔软的心"; str=str.replaceAll("总会在.+?流年", "总会有某一个回眸的时刻醉了流年"); System.out.println(str); } } 注:
假设要匹配${2}中间为数字的这个类型的变量String,则 Pattern p = Pattern.compile("\\$\\{\\d+\\}"); Matcher m = p.matcher(String); boolean b = m.find(); while (b) { //后续操作…… }
import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; public class test { public void main() { getStrings(); //用正则表达式获取指定字符串内容中的指定内容 System.out.println("********************"); replace(); //用正则表达式替换字符串内容 Sys
有一道这样的面试题 写一个Java方法,利用正则表达式判断输入str中包含字符串”ios“或”apple“(大小写不敏感),但不包括”mediaplayer“.如果满足条件,返回所包含的字符串”ios”和/或”apple“(按实际大小写返回) 解决办法: package com.xfma.demo; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static v