Find phoneNumbers in text with regex [duplicate]










-1
















This question already has an answer here:



  • What do ^ and $ mean in a regular expression?

    2 answers



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?\d2)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.










share|improve this question













marked as duplicate by Wiktor Stribiżew java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 17:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 5





    Try removing the anchors ^ and $

    – The fourth bird
    Nov 12 '18 at 17:06















-1
















This question already has an answer here:



  • What do ^ and $ mean in a regular expression?

    2 answers



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?\d2)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.










share|improve this question













marked as duplicate by Wiktor Stribiżew java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 17:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 5





    Try removing the anchors ^ and $

    – The fourth bird
    Nov 12 '18 at 17:06













-1












-1








-1









This question already has an answer here:



  • What do ^ and $ mean in a regular expression?

    2 answers



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?\d2)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.










share|improve this question















This question already has an answer here:



  • What do ^ and $ mean in a regular expression?

    2 answers



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?\d2)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.





This question already has an answer here:



  • What do ^ and $ mean in a regular expression?

    2 answers







java regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 17:04









user1007522user1007522

3,22774591




3,22774591




marked as duplicate by Wiktor Stribiżew java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 17:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Wiktor Stribiżew java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 17:28


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 5





    Try removing the anchors ^ and $

    – The fourth bird
    Nov 12 '18 at 17:06












  • 5





    Try removing the anchors ^ and $

    – The fourth bird
    Nov 12 '18 at 17:06







5




5





Try removing the anchors ^ and $

– The fourth bird
Nov 12 '18 at 17:06





Try removing the anchors ^ and $

– The fourth bird
Nov 12 '18 at 17:06












2 Answers
2






active

oldest

votes


















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.






share|improve this answer























  • Omg I feel so stupid.

    – user1007522
    Nov 12 '18 at 17:33


















0














Try using this



var telephone = /(?s?+?32s?)?s?[789]d8,/;


I’ve not tried it before.






share|improve this answer























  • var phone = “+327890005678”; telephone.match(phone);

    – Jolaosho batmat
    Nov 12 '18 at 17:16


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer























  • Omg I feel so stupid.

    – user1007522
    Nov 12 '18 at 17:33















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.






share|improve this answer























  • Omg I feel so stupid.

    – user1007522
    Nov 12 '18 at 17:33













3












3








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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 17:14









Devin LedesmaDevin Ledesma

463




463












  • Omg I feel so stupid.

    – user1007522
    Nov 12 '18 at 17:33

















  • Omg I feel so stupid.

    – user1007522
    Nov 12 '18 at 17:33
















Omg I feel so stupid.

– user1007522
Nov 12 '18 at 17:33





Omg I feel so stupid.

– user1007522
Nov 12 '18 at 17:33













0














Try using this



var telephone = /(?s?+?32s?)?s?[789]d8,/;


I’ve not tried it before.






share|improve this answer























  • var phone = “+327890005678”; telephone.match(phone);

    – Jolaosho batmat
    Nov 12 '18 at 17:16
















0














Try using this



var telephone = /(?s?+?32s?)?s?[789]d8,/;


I’ve not tried it before.






share|improve this answer























  • var phone = “+327890005678”; telephone.match(phone);

    – Jolaosho batmat
    Nov 12 '18 at 17:16














0












0








0







Try using this



var telephone = /(?s?+?32s?)?s?[789]d8,/;


I’ve not tried it before.






share|improve this answer













Try using this



var telephone = /(?s?+?32s?)?s?[789]d8,/;


I’ve not tried it before.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 17:13









Jolaosho batmatJolaosho batmat

94




94












  • var phone = “+327890005678”; telephone.match(phone);

    – Jolaosho batmat
    Nov 12 '18 at 17:16


















  • var phone = “+327890005678”; telephone.match(phone);

    – Jolaosho batmat
    Nov 12 '18 at 17:16

















var phone = “+327890005678”; telephone.match(phone);

– Jolaosho batmat
Nov 12 '18 at 17:16






var phone = “+327890005678”; telephone.match(phone);

– Jolaosho batmat
Nov 12 '18 at 17:16




Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20