如何使用正则表达式从java中的短语中检查和提取信用卡号

如何使用正则表达式从java中的短语中检查和提取信用卡号,java,regex,Java,Regex,假设我有一根绳子 “我的信用卡号码是44444444448,等等……” 如何使用正则表达式从整个字符串中提取数字 我使用的VISA的正则表达式模式是 "^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$" 但我不知道如何使用笨重的绳子。我是regex的新手。 Thanx all, 我尝试了下面的代码,它的效果非常好 public static void main(String args[]){ String ccPattern = "((?:(?

假设我有一根绳子

“我的信用卡号码是44444444448,等等……”

如何使用正则表达式从整个字符串中提取数字

我使用的VISA的正则表达式模式是

"^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$"
但我不知道如何使用笨重的绳子。我是regex的新手。

Thanx all, 我尝试了下面的代码,它的效果非常好

public static void main(String args[]){
    String ccPattern = "((?:(?:\\d{4}[- ]){3}\\d{4}|\\d{16}))(?![\\d])";
    Pattern pattern = Pattern.compile(ccPattern);
    Matcher matcher = pattern.matcher("My credit card number is 4444-4444-4444-4448.");
    if(matcher.find()){
        System.out.println("Pattern matches");
        System.out.println(matcher.group(0));
    }
    else{
        System.out.println("Does not matches");
    }
}

\\b\\d{15,16}\\b
将涵盖您可以看到的大部分信用卡帮助:@Akshada Gaikwad查看并
public static void main(String args[]){
    String ccPattern = "((?:(?:\\d{4}[- ]){3}\\d{4}|\\d{16}))(?![\\d])";
    Pattern pattern = Pattern.compile(ccPattern);
    Matcher matcher = pattern.matcher("My credit card number is 4444-4444-4444-4448.");
    if(matcher.find()){
        System.out.println("Pattern matches");
        System.out.println(matcher.group(0));
    }
    else{
        System.out.println("Does not matches");
    }
}