Find phoneNumbers in text with regex [duplicate] - java

This question already has answers here:
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 4 years ago.
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.

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.

Try using this
var telephone = /\(?s?+?32s?\)?s?[789]d{8,}/;
I’ve not tried it before.

Related

How to extract sub string by matching the known set of keyword(s) [duplicate]

This question already has answers here:
Regex match one of two words
(2 answers)
Closed 2 years ago.
Trying to extract substring after a particular code for example
String sample1 = "/ASDF/096/GHJKL/WER/WER/dv/7906/CODEM/TEAR1331927498xxxxxx/YUII/OPL";
String sample2 = "/CODEM/TEAR1331927498xxxxxx";
String regExpresssion = "[/CODEM/]{6}(^[a-zA-Z0-9|\\s])?";
final Pattern pattern = Pattern.compile(regExpresssion);
final Matcher matcher = pattern.matcher(sample1);
if (matcher.find()) {
String subStringOut = sample1.substring(matcher.end());
}
subStringOut for sample 1 > TEAR1331927498xxxxxx/YUII/OPL
subStringOut for sample 2 > TEAR1331927498xxxxxx
above code is working fine but now I need to add one more identifier '/CODER/' in regex expression for below sample
String sample3 = "/ASDF/096/GHJKL/WER/WER/dv/7906/CODER/TEAR1331927498xxxxxx/YUII/OPL";
I have tried
String regExpresssion = "[/CODEM/|/CODER/]{6}(^[a-zA-Z0-9|\\s])?";
but it is not working. Any suggestions guys?
Thanks!!
try replacing [/CODEM/|/CODER/]{6} with /CODE[RM]/
I think you meant to match the entire phrase /CODEM/ or /CODER/ but because of the way you wrote it you were accepting any sequence of any of those characters 6 characters long. I'm not entirely sure though. The Brackets represent a "character class" and they only match a single character, if you want to match multiple in a row you use parentheses. Also the second part does not make sense to me because the exponent sign is in the middle of the phrase, and in that context it matches the beginning of a line.
Just need single look behind assersun
Try (?<=/CODE[MR]/).*
PCRE demo
but works for Java in this case

RegEx ignore letters until digit occurs and then match letters afterwards [duplicate]

This question already has answers here:
Using Regular Expressions to Extract a Value in Java
(13 answers)
Closed 3 years ago.
This is intended to be used in Java.
Imagine following sample input:
WRA1007
1085808
1092650S
3901823CV
I want to match all alphabetic characters after at least one digit.
Desired output:
S
CV
Actual output:
0S
3CV
My current approach looks like this:
\d[a-zA-Z]+
The problem with this pattern is that it includes the digit beforehand too. My current solution is to remove the first character of the resulting string afterwards. And this seems quite unsatisfactory to me.
You need a lookbehind:
(?<=\d)[a-zA-Z]+
(?<=\d) means "there must be a digit before this position, but don't match it".
Demo
Alternatively, you can use a pair of () to surround the part you want to get:
\d([a-zA-Z]+)
This is called a "group", and you can get its value by calling group(1) on your Matcher.
If you 'add' groups you can get group 1 that contain only letters
\d([a-zA-Z]+)

Java regex for consecutive exponential terms [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Regex to first occurrence only? [duplicate]
(4 answers)
Closed 4 years ago.
I've tested this regex:
String regex = "[e][#](.)+[$]
the regex works well to identify exponential terms, however it breaks when there are two or more consecutive exponential terms.
I test the regex with the usual code:
while(matcher.find()){
String string = matcher.group();
System.out.println("this one: "+string);
}
When I type the expression:
e#x$ + 3e#x+1$
string equals to (e#(x$+3e#x+1$))
By the way, I added the parentheses inside the while loop. They are necessary for
what I am trying to accomplish.
I want the result of string to be (e(#x$))+3(e(#x+1$)
I know the problem lies in "(.)+ What I think is happening is that the regex
includes the first $, what I need is for it to stop at the first $.
How can I include this logic inside the regex?
thank you

Java Regex: Matching Multiple Occurrences [duplicate]

This question already has answers here:
How to get multiple regex matches in Java?
(2 answers)
Closed 3 years ago.
I have a list of phone numbers and other text such as the following:
+1-703-535-1039 +1-703-728-8382 +1-703-638-1039 +1-703-535-1039
And I'm trying to match just the area code and first 3 digits of the number.
Currently I'm using the following Regex:
\d{3}-\d{3}
But it only returns the first match instead of all matches.
Pls see this link for reference:
https://regex101.com/r/oO1lI9/1
In regex101, use global g flag to get all matches
Demo
To get all matches in Java:
Pattern pattern = Pattern.compile("(\d{3}-\d{3})");
Matcher matcher = pattern.matcher("+1-703-535-1039 +1-703-728-8382 +1-703-638-1039 +1-703-535-1039");
// Find all matches
while (matcher.find()) {
// Get the matching string
String match = matcher.group();
}
Reference

REgular Expression using Java [duplicate]

This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 8 years ago.
I am Using Regular Expression to break the string, I am trying to break the string but In reqular Expressions I am missing some format. Can any one please let me know where i went wrong.
String betweenstring="['Sheet 1$'].[DEPTNO] AS [DEPTNO]";
System.out.println("betweenstring: "+betweenstring);
Pattern pattern = Pattern.compile("\\w+[.]\\w+");
Matcher matchers=pattern.matcher(betweenstring);
while(matchers.find())
{
String filtereddata=matchers.group(0);
System.out.println("filtereddata: "+filtereddata);
}
I need to break like this:
['Sheet 1$']
[DEPTNO] AS [DEPTNO]
Given your very specific input, this regex works.
([\w\[\]' $]+)\.([\w\[\]' $]+)
Capture group one is before the period, capture group 2, after. To escape this for a Java string:
Pattern pattern = Pattern.compile("([\\w\\[\\]' $]+(\\.*[\\w\\[\\]' $]+)");
However, it would be much easier to split the string on the literal dot, if this is what you are trying to achieve:
String[] pieces = between.split("\\.");
System.out.println(pieces[0]);
System.out.println(pieces[1]);
Output:
['Sheet 1$']
[DEPTNO] AS [DEPTNO]

Categories

Resources