简体   繁体   中英

Restricting digits in a regex match

tl;dr : How can I set a count constraint on a particular token in a regex.

(regex-exp){constraint:max 10 only digits}

I have been trying to find a phone number in a text block. Android platforms Patterns class gives reasonable coverage. But the primary issue it has it

  • It does not have a min length
  • It does not have a max length

So it actually matches even 1234 and also when there is a string like my phone numbers are +19447223311 872881122 , it matches both the numbers as a single number. If we can add a constraint that the pattern should have digits {7,12}, it will solve for both I guess. As much I tried couldn't make it work. Here is the regex for the pattern.

public static final Pattern PHONE
    = Pattern.compile(                      // sdd = space, dot, or dash

            "(\\+[0-9]+[\\- \\.]*)?"        // +<digits><sdd>*

            + "(\\([0-9]+\\)[\\- \\.]*)?"   // (<digits>)<sdd>*

            + "([0-9][0-9\\- \\.]+[0-9])"); // <digit><digit|sdd>+<digit>

There is no such thing like regex constraint in Android and as the PHONE pattern is pre-compiled, you can't extend it either.

I would do it like this:

Pattern constraint = Pattern.compile("\\+?\\d{7,12}");
Matcher constraint_matcher = constraint.matcher("I will be home by 12:30 and if you have anything " +
        "urgent call me at 901 101 0101, and my extension is 1021 +19447223311  872881122    ");

while (constraint_matcher.find()) {

    Matcher phone_number_matcher = Patterns.PHONE.matcher(constraint_matcher.group(0));

    if (phone_number_matcher.find()) {
        Log.d("MATCH", phone_number_matcher.group(0));
    }
}

which gives this result:

09-15 16:20:34.277 10119-10119/com.example D/MATCH: +19447223311
09-15 16:20:34.277 10119-10119/com.example D/MATCH: 872881122

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM