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>