-1

I'm having a whole text in a string and I want to find all belgium cell phone numbers.

So I wrote this piece of code:

Pattern cellPhoneRegex = Pattern.compile("^((\\+|00)32\\s?|0)4(60|[789]\\d)(\\s?\\d{2}){3}$");
    List<String> cellPhoneList = new ArrayList<>();
    Matcher cellPhoneMatches = cellPhoneRegex.matcher("+32495715511");
    while (cellPhoneMatches.find()) {
        cellPhoneList.add(cellPhoneMatches.group());
    }

    System.out.println(cellPhoneList);

Now the thing is that when you run this it matches the phone number. But when the same number is in a huge text it doesn't find anything.

For this string "Tel: +32495715511" there are no matches.

I don't see why it's not matching.

user1007522
  • 7,040
  • 14
  • 62
  • 107

2 Answers2

3

Exactly what @Thefourthbird said. You're regex is looking for an exact match. As in the text to match has to start with (^ means starts with in this example) and end with ($ means ends with in this example) the phone number matching the regex.

0

Try using this

var telephone = /\(?s?+?32s?\)?s?[789]d{8,}/;

I’ve not tried it before.