Find phoneNumbers in text with regex [duplicate]
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.
java regex
marked as duplicate by Wiktor Stribiżew
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.
add a comment |
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.
java regex
marked as duplicate by Wiktor Stribiżew
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
add a comment |
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.
java regex
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
java regex
asked Nov 12 '18 at 17:04
user1007522user1007522
3,22774591
3,22774591
marked as duplicate by Wiktor Stribiżew
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
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
Omg I feel so stupid.
– user1007522
Nov 12 '18 at 17:33
add a comment |
Try using this
var telephone = /(?s?+?32s?)?s?[789]d8,/;
I’ve not tried it before.
var phone = “+327890005678”; telephone.match(phone);
– Jolaosho batmat
Nov 12 '18 at 17:16
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Omg I feel so stupid.
– user1007522
Nov 12 '18 at 17:33
add a comment |
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.
Omg I feel so stupid.
– user1007522
Nov 12 '18 at 17:33
add a comment |
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.
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.
answered Nov 12 '18 at 17:14
Devin LedesmaDevin Ledesma
463
463
Omg I feel so stupid.
– user1007522
Nov 12 '18 at 17:33
add a comment |
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
add a comment |
Try using this
var telephone = /(?s?+?32s?)?s?[789]d8,/;
I’ve not tried it before.
var phone = “+327890005678”; telephone.match(phone);
– Jolaosho batmat
Nov 12 '18 at 17:16
add a comment |
Try using this
var telephone = /(?s?+?32s?)?s?[789]d8,/;
I’ve not tried it before.
var phone = “+327890005678”; telephone.match(phone);
– Jolaosho batmat
Nov 12 '18 at 17:16
add a comment |
Try using this
var telephone = /(?s?+?32s?)?s?[789]d8,/;
I’ve not tried it before.
Try using this
var telephone = /(?s?+?32s?)?s?[789]d8,/;
I’ve not tried it before.
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
add a comment |
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
add a comment |
5
Try removing the anchors
^
and$
– The fourth bird
Nov 12 '18 at 17:06