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

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

Related

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 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.

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

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.

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