标签:you 出现 一个 模式 inpu case code dsd i++
. 任意一个字符
? 被修饰的模式出现0/1次
String
Pattern
Matcher
boolean matches?(String regex)
检查是否能匹配整个字符串
System.out.println("123".matches("\\d+"));//1
System.out.println("abc123".matches("\\w+"));//1
System.out.println(",./123abc".matches("\\w+"));//0
//Pattern:
static Pattern compile?(String regex)
static Pattern compile?(String regex, int flags)//设置匹配的模式,例如不区分大小写
Matcher matcher?(CharSequence input)//根据input生成Matcher
static boolean matches?(String regex, CharSequence input)//input整个匹配regex
String[] split?(CharSequence input)//
//Matcher:
Matcher appendReplacement?(StringBuffer sb, String replacement)
StringBuffer appendTail?(StringBuffer sb)
boolean find()
String group()
String group?(int group)
boolean matches()
String replaceAll?(String replacement)
//要匹配的正则表达式
String regex="\\b(\\w|\\.)+@\\w+(\\.\\w+)+\\b";
//被匹配的字符串
String source="My email is abcde.12345@163.com.";
//编译正则表达式
Pattern pattern=Pattern.compile(regex);
//根据源字符串生成Matcher
Matcher matcher=pattern.matcher(source);
//依次匹配子串
while(matcher.find())
{
//本次被匹配到的字串
System.out.println(matcher.group());
}
//全部替换为一样的
string.replaceAll(regex,replacement)
//区分替换的位置
Pattern pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);//不区分大小写
String source = "java Java jAva ILoveJavA youHateJAVA adsdsfd";
Matcher matcher = pattern.matcher(source);
StringBuilder builder = new StringBuilder();
int i = 0;
while (matcher.find()) {
i++;
if (i % 2 == 1)
matcher.appendReplacement(builder, "you");
else
matcher.appendReplacement(builder, "me");
}
matcher.appendTail(builder);
System.out.println(builder);
while(matcher.find())
sout(matcher.group())
string.replaceAll(regex,replacement)
或者
matcher.replaceAll()
或者
matcher.appnedReplacement(stringBuilder,replacement)
while(matcher.find())
sout(matcher.group(index))
标签:you 出现 一个 模式 inpu case code dsd i++
原文地址:https://www.cnblogs.com/darknessplus/p/10331555.html