我正在尝试输入之间的内容,我的模式没有做正确的事,请帮忙。
下面是sudocode:
s="Input one Input Two Input Three";
Pattern pat = Pattern.compile("Input(.*?)");
Matcher m = pat.matcher(s);
if m.matches():
print m.group(..)
要求的输出:
之一
二
三
先行使用Input
并find
在循环中使用,而不是matches
:
Pattern pattern = Pattern.compile("Input(.*?)(?=Input|$)");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
看到它在线上工作:ideone
但是最好在这里使用split:
String[] result = s.split("Input");
// You need to ignore the first element in result, because it is empty.
看到它在线上工作:ideone
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句