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

问题描述 投票:0回答:1

在我的 Android 文本中,我想根据特殊模式从文本中提取所有数字,例如 They are from 15 to 20 digits 。像 Python 中的 findall() 方法:

re.findall(r"\d{15,20}", r.text)
java android regex nsregularexpression
1个回答
0
投票

您可以尝试使用下一个代码片段:

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());
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.