具有前瞻功能的 Java 正则表达式
有没有办法在java中打印出正则表达式模式的前瞻部分?
String test = "hello world this is example";
Pattern p = Pattern.compile("\\w+\\s(?=\\w+)");
Matcher m = p.matcher(test);
while(m.find())
System.out.println(m.group());
这段代码打印出来:
你好
世界
这是
我想要做的是将单词作为对打印:
你好世界
世界这个
这个 是
是示例
我该怎么做?
is there a way to print out lookahead portion of a regex pattern in java?
String test = "hello world this is example";
Pattern p = Pattern.compile("\\w+\\s(?=\\w+)");
Matcher m = p.matcher(test);
while(m.find())
System.out.println(m.group());
this snippet prints out :
hello
world
this
is
what I want to do is printing the words as pairs :
hello world
world this
this
is
is example
how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地将捕获括号放在先行表达式内:
You can simply put capturing parentheses inside the lookahead expression: