Java Regexp Matcher

use*_*196 1 java regex matcher

关于regexp的简单问题.我有

String text = "foobar1foobar1";
Run Code Online (Sandbox Code Playgroud)

我需要在第一个之前获得一部分(foobar)当我做这样的事情时:

Pattern date_pattern = Pattern.compile("(.+)1");
Matcher matcher = date_pattern.matcher(text);
matcher.group(1);
Run Code Online (Sandbox Code Playgroud)

但我收到"foobar1foobar".

bee*_*jay 7

+量词是贪婪的,因此尽可能多的匹配越好.你应该是这个量词的不情愿的版本+? ; 你的模式然后变成:

(.+?)1
Run Code Online (Sandbox Code Playgroud)