(3)正则补充
来源:互联网 发布:照片漫画化软件 编辑:程序博客网 时间:2024/06/08 13:26
1. 量词的小细节,贪婪与懒惰
2. find()/find(i)方法
3. (?m) 单行模式 :标记的作用及用法(了解,用到时扩展)
import java.util.regex.Matcher;import java.util.regex.Pattern;public class TestRegex {public static void main(String[] args) {String str = "abcabcabc";System.out.println(str.replaceFirst("(abc)+", "---")); //abc出现一次或多次System.out.println(str.replaceFirst("abc+", "---")); //ab后面的C出现一次或多次System.out.println(str.replaceFirst("\\S+?", "---")); //贪婪模式,尽力匹配System.out.println(str.replaceFirst("\\S+", "---")); //懒惰模式, 最少匹配StringBuilder sb = new StringBuilder();Matcher m = Pattern.compile("\\w+").matcher("Evening is full of the linnet's wings");while(m.find()) {sb.append(m.group()).append(" ");}System.out.println(sb.toString());sb.setLength(0);int i = 0;while(m.find(i)) { //设置find的起点sb.append(m.group()).append(" ");i++;}System.out.println(sb.toString());sb.setLength(0);String str2 = "i have a big ice\n" + "you not have ice\n";//(?m)启动单行模式,此时^$符号表示每行的开头结尾,而不是整个字符串的开头结尾Matcher m2 = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(str2);while(m2.find()) {for(int j=0; j<=m2.groupCount(); j++) {sb.append("[").append(m2.group(j)).append("]");}System.out.println(sb.toString());sb.setLength(0);}//find matches lookingAt区别}}
//output------abcabc---bcabcabc---Evening is full of the linnet s wings Evening vening ening ning ing ng g is is s full full ull ll l of of f the the he e linnet linnet innet nnet net et t s s wings wings ings ngs gs s [a big ice][a][big ice][big][ice][not have ice][not][have ice][have][ice]
4. appendReplacement() 、appendTail()
5.reset()
import java.util.regex.Matcher;import java.util.regex.Pattern;public class TestRegex2 {public static void main(String[] args) {Pattern p = Pattern.compile("cat");Matcher m = p.matcher("one cat two cats in cat the cat yard cat");StringBuffer sb = new StringBuffer();int i = 0;while (m.find()) {if (i % 2 == 0) {m.appendReplacement(sb, "dog");}i++;}m.appendTail(sb);System.out.println(sb.toString());sb.setLength(0);// reset()重新matcher字符串m.reset("one cat two cats in the yard");while (m.find()) {m.appendReplacement(sb, "dog");}m.appendTail(sb);System.out.println(sb.toString());}}
//outputone dog two cats in dog the cat yard dogone dog two dogs in the yard
- (3)正则补充
- 正则表达式(补充)
- 正则表达式使用方法(补充)
- 正则补充
- 正则表达式常用(持续补充)
- 正则表达式,从零开始(不断补充)
- 正则知识点补充
- 正则表达式补充学习
- PHP 正则补充
- 正则表达式补充
- javascript 正则表达式补充
- javascript 正则表达式补充
- 安卓正则补充
- Django补充(3)
- 常见的正则表达式(不足的请补充)
- 禁止表单的自动完成(正则表达式的补充)
- 一些常用的正则表达式(待补充)
- JavaScript正则快速教程(待续,不断更新补充)
- 日期前五年后五年ORACLE
- subMaxSum
- 第十五周项目-带姓名的成绩单
- sql查询动态表名的记录
- jsp学习笔记——跳转命令
- (3)正则补充
- 正则表达式30分钟入门教程(键人岐)
- OCP-1Z0-053-V13.02-276题
- 电脑文件恢复的具体操作
- lamdba算子之布尔值理解
- 程序员不能不懂数据存储
- 获取字符窜的字节长度
- 数据库基础知识
- cocos2d-x 使用CCLabelAtlas自定义字体