正则表达式java [英] regular expression java

查看:117
本文介绍了正则表达式java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试输入Input之间的内容,我的模式做得不对,请帮忙。

I am trying to Take the content between Input, my pattern is not doing the right thing please help.

下面是sudocode:

below is the sudocode:

s="Input one Input Two Input Three";
Pattern pat = Pattern.compile("Input(.*?)");
Matcher m = pat.matcher(s);

 if m.matches():

   print m.group(..)

必需输出:

一个

两个

推荐答案

使用预测输入并在循环中使用 find ,而不是匹配

Use a lookahead for Input and use find in a loop, instead of matches:

Pattern pattern = Pattern.compile("Input(.*?)(?=Input|$)");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
   System.out.println(matcher.group(1));
}

看到它在线工作: ideone

但最好在这里使用split:

But it's better to use split here:

String[] result = s.split("Input");
// You need to ignore the first element in result, because it is empty.

查看在线工作: ideone

这篇关于正则表达式java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆