Java Regex: Matching Multiple Occurrences [duplicate] - java

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

Related

How to get value inside many regex keys? [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I am trying to get the values ​​that are in braces as below:
{{{{{{{{{{{{{{{{{{value1 here!}}}}}}}}}}}{value2 here!}}}}}}}}}{value3 here!}}}}}}
I would like you to be selected as a group:
{value1 here!} {value2 here!} {value3 here!}
I'm doing the expression on the site Regex101 and applying it to my Java project.
Pattern p = Pattern.compile("\\\\{.*\\\\}");
Matcher m = p.matcher("{{{{{{{{{{{{{{{{{{value1 here!}}}}}}}}}}}{value2 here!}}}}}}}}}{value3 here!}}}}}}");
if (m.find()){
String value1 = m.group(1);
String value2 = m.group(2);
String value3 = m.group(3);
}
How can I solve this problem?
You just need your glob match to match on non-brace characters:
\{[^{}]*\}
Regex101
This is accomplished by using the construct [^...] for negated character classes which matches all characters except the ones specified within the [^ and ] delimiters

Java getting substring from square brackets [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I am trying to get a substring from square brackets in a string, I tried multiple regexes but nothing works.
I tried multiple regexes and code from multiple stackoverflow posts but nothing works.
String mydata = "some string with [the data i want] inside";
Pattern pattern = Pattern.compile("[(.*?)]");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
System.out.println(matcher.group(1));
output = output.replace("%item%", matcher.group(1));
}
It should return "the data i want" but instead it says that no pattern was found.
You need to escape the [] characters so they are seen as literal square brackets.
Using this as your regex should work: \\[(.*?)\\]

Find phoneNumbers in text with regex [duplicate]

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.

Find all non unicode characters in file [duplicate]

This question already has answers here:
How to check if a String contains only ASCII?
(14 answers)
Closed 6 years ago.
So i have a textfile that includes non unicode characters.
For example
"pr�s-*"
ESt Präs
How do I print them out, but only them. I know this java method for replacing it
String resultString = currentLine.replaceAll("[^\\x00-\\x7F]", "");
I dont want to replace them, I want to find them and print it out.
You may use a Matcher#find to find and print all these non-ASCII chars with your [^\\x00-\\x7F] regex or \\P{ASCII}:
String s = "pr�s-*";
Pattern pattern = Pattern.compile("\\P{ASCII}");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
System.out.println(matcher.group(0));
}
See the Java demo
See Java regex reference:
\p{ASCII} = All ASCII:[\x00-\x7F]
And \P means a reverse class, all chars other than ASCII.

using if statment with regular expressions to know if String contains specified characters or not in java [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 7 years ago.
i have made this code and i don't know what is the problem with it, why the output print " f "??? even that the string contains the specified characters in the regex
String s="x^2+x-20";
Pattern pattern =
Pattern.compile("([+-][0-9]*)(([a-z A-Z])\\^2)"); //regex
Matcher matcher = pattern.matcher(s);
if(matcher.matches()){
System.out.println("t");
} else {
System.out.println("f");}
Your regex makes [+-] not an optional match but rather required. Use the following:
"([+-]?[0-9]*)(([a-z A-Z])\\^2)" // java syntax
Or as a regex without any java escaping:
([+-]?[0-9]?)(([a-z A-Z])\^2)
Also use Matcher.find rather than Matcher.match to find matches that are not the entire string.

Categories

Resources