Java getting substring from square brackets [duplicate] - java

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: \\[(.*?)\\]

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

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.

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]

How to write a regex for words like [[gold]] or [[Archimedes]] [duplicate]

This question already has answers here:
java regular expression to extract content within square brackets
(3 answers)
Closed 8 years ago.
I have an array of strings. I am checking for the following pattern in each element of the array: [[some word]].
The element should start with two square brackets and end with 2 square brackets with a word or a sentence in between them.
I also need to extract the string "some word" from [[some word]]. I am unable to figure out the regular expressions in Java. They appear to be very different from scripting language like PHP.
Eg. If I encounter [[this is an example]] while traversing the array, I should output "this is an example". This should be done for all strings enclosed in double square brackets.
The following code:
String input = "How to write a regex for words like [[gold]] or [[Archimedes]] in JAVA";
String regex = "\\[\\[(.*?)\\]\\]";
Matcher matcher = Pattern.compile(regex).matcher(input);
int idx=0;
while(matcher.find(idx)){
String match = matcher.group(1);
System.out.println(match);
idx = matcher.end();
}
prints:
gold
Archimedes
The actual regex (without escaped \) is:
\[\[(.*?)\]\]
I believe this regex should work for you:
"(?s)\\[\\[(.*?)\\]\\]"
This will also grab multi line sentence between [[ and ]] because of use of (?s) (DOTALL)
\\[\\[([^]]*)\\]\\] can be used as a slight variation on #anubhava's answer. As the comments here point out, it won't catch [[a]b]], but I couldn't tell whether that was part of your requirement or not.

Categories

Resources