在Android中使用正则表达式搜索文本



在我的文本中在Android上,我想根据特殊的模式从文本中提取所有的数字,例如,它们是从15到20位数字。类似于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());
}
}
}

最新更新