正则表达式 分组 还有\\1 总结。。

转载来自:http://bbs.itheima.com/thread-7177-1-1.html

"(\w)((?=\1\1\1)(\1))+

1. (\w) -- \w,一个字符,括号表示一个子匹配,第一个括号是"\1",第二个括号是"\2",……。
2. (\w)(\1) -- 一个字符,后面紧跟一个相同的字符。

  1.                 // Pattern pattern = Pattern.compile("(\\w)((?=\\1\\1\\1)(\\1))+");
  2.                 Pattern pattern = Pattern.compile("(\\w)(\\1)");
  3.                 Matcher matcher = pattern.matcher("aaa ffffff 999999999");
  4.                 // Matcher matcher = pattern.matcher("aaa 999999");
  5.                 while (matcher.find()) {
  6.                         System.out.print(matcher.group()+" | ");
  7.                 }

("aaa ffffff 999999999");aa | ff | ff | ff | 99 | 99 | 99 | 99 |

2. (\\w)(?=\\1\\1\\1)
 Pattern.compile("(\\w)(?=\\1\\1\\1)");

运行得:f | f | f | 9 | 9 | 9 | 9 | 9 | 9 | ,

四个字符连续相同才符合条件,且 index只+1(6个 f 所以是3个了)

如无“?=”:(\\w)(\\1\\1\\1),则是从第五个开始 ffffff 输出:f | xxx)

后面继续验证的,看懵了,先码后看。。。

发布了28 篇原创文章 · 获赞 5 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/java_utf8/article/details/69808727