Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String regex = "\\b(?<areaCode>\\d{3})(?<prefix>\\d{3})(?<postPhoneNumber>\\d{4})\\b";
        String source = "1234567890, 12345, and 9876543210";
        Pattern p = Pattern.compile(regex);

        Matcher m = p.matcher(source);
        while (m.find()) {
            String matchedText = m.group();
            int start1 = m.start("areaCode");
            int start2 = m.start("prefix");
            int start3 = m.start("postPhoneNumber");
            System.out.println("Matched Text:" + matchedText);
            System.out.println("Area code start:" + start1);
            System.out.println("Prefix start:" + start2);
            System.out.println("Line Number start:" + start3);
        }
    }
}