在我的 Android 文本中,我想根据特殊模式从文本中提取所有数字,例如 They are from 15 to 20 digits 。像 Python 中的 findall() 方法:
re.findall(r"\d{15,20}", r.text)
您可以尝试使用下一个代码片段:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp{
public static void main(String[] args) {
String text = "test with 111222333444555, 12345 and 11223344556677889900 numbers";
// matches digits that are between 15 to 20 digits long
String pattern = "\\d{15,20}";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
while (m.find()) {
System.out.println(m.group());
}
}
}