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.
Related
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
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: \\[(.*?)\\]
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.
This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 7 years ago.
I have the unproper data in this way. i need to extract the data before dot and after dot symbol using regular expression. I am using but i am not able to get exact data. please help. It is very urgent
Code:
Matcher matcher = Pattern.compile("([\\w[\\$##\\-^&]\\w\\[\\]' $]+)\\.([\\w\\[\\]' $]+)").matcher(formulaData);
while (matcher.find())
{
String Data=matcher.group(0);
String[] pieces = Data.split("\\.");
Heading=pieces[0].replace("\"", "");
Heading=pieces[1].replace("\"", "");
}//while
You can split by newline and then split by dot
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]