0

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>
Codevalley
  • 4,593
  • 7
  • 42
  • 56
  • Possible duplicate of [Java Regular Expressions to Validate phone numbers](https://stackoverflow.com/questions/42104546/java-regular-expressions-to-validate-phone-numbers) – Michael Sharp Sep 01 '17 at 02:18
  • Phone numbers don't really have a max length. Some companies especially will use extra digits if it spells something, as the extra numbers are ignored – Gabe Sechan Sep 01 '17 at 02:32
  • @GabeSechan Maybe yeah. But in my case, I want to know how to put a count constraint on a regex. :-\ – Codevalley Sep 01 '17 at 04:51
  • The other problem is that a rehex isn't the right tool for this. The language to parse a phone number isn't regular. – Gabe Sechan Sep 01 '17 at 04:53
  • the emphasis is not on the entity "phone number". How can I add a constraint to a regex on count of a particular token – Codevalley Sep 01 '17 at 04:54
  • Without input and expected output I will go like this: Strip your string for anything that is not a number, count the char. – Drag and Drop Sep 01 '17 at 06:51
  • Won't work. Say my string is "I will be home by 12:30 and if you have anything urgent call me at 901 101 0101, and my extension is 1021" The result will be 123090110101011021 – Codevalley Sep 01 '17 at 07:09

1 Answers1

0

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
CarHa
  • 1,148
  • 11
  • 31