您的位置:首页 > 其它

文章标题

2016-04-21 09:11 197 查看
①字符匹配

Pattern p = Pattern.compile(expression); // 正则表达式

Matcher m = p.matcher(str); // 操作的字符串

boolean b = m.matches(); //返回是否匹配的结果

System.out.println(b);

Pattern p = Pattern.compile(expression); // 正则表达式

Matcher m = p.matcher(str); // 操作的字符串

boolean b = m. lookingAt (); //返回是否匹配的结果

System.out.println(b);

Pattern p = Pattern.compile(expression); // 正则表达式

Matcher m = p.matcher(str); // 操作的字符串

boolean b = m..find (); //返回是否匹配的结果

System.out.println(b);

②分割字符串

Pattern pattern = Pattern.compile(expression); //正则表达式

String[] strs = pattern.split(str); //操作字符串 得到返回的字符串数组

③替换字符串

Pattern p = Pattern.compile(expression); // 正则表达式

Matcher m = p.matcher(text); // 操作的字符串

String s = m.replaceAll(str); //替换后的字符串

④查找替换指定字符串

Pattern p = Pattern.compile(expression); // 正则表达式

Matcher m = p.matcher(text); // 操作的字符串

StringBuffer sb = new StringBuffer();

int i = 0;

while (m.find()) {

m.appendReplacement(sb, str);

i++; //字符串出现次数

}

m.appendTail(sb);//从截取点将后面的字符串接上

String s = sb.toString();

⑤查找输出字符串

Pattern p = Pattern.compile(expression); // 正则表达式

Matcher m = p.matcher(text); // 操作的字符串while (m.find()) {

matcher.start() ;

matcher.end();

matcher.group(1);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: