Pattern和Matcher中正宗正则表达式
程序员文章站
2022-04-22 16:43:30
...
package day_15; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Group { public static final String POEM="Twas brillig,and the slity toaves\n"+ "Did gyre and gimble in the wabe.\n"+ "All mimsy were the borogoves.\n"+ "And the mome raths outgrabe.\n\n"; /**第一层group表示输出最后三个单词 * @param args * 表示结尾的标志;?m表示开头的 标志 * m.groupCount()组数不包括最外层 的即group(0)是不被计数的; */ public static void main(String[] args) { /*String s="123546378645"; System.out.println(s.replaceAll("1\\d{10}", s));*/ Matcher m=Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(POEM); while(m.find()){ for (int i = 0; i <=m.groupCount(); i++) { System.out.print("["+m.group(i)+"]"); System.out.println(m.start(i)); System.out.println(m.end(i)); System.out.println(); } } } }
下一篇: 上传图片添加水印?该怎么解决