I am having problem to split string in java. it gives java.util.regex.Pattern.error.
String name = One\Two\Three.
String[] str = name.split("\\");
for(int i =0; i < str.length ; i++)
System.out.println(str[i]);
I put another \ as escape character but not working.
help me.
One\Two\Three is not a valid string literal (you need quotes and you need to escape the backslashes).
String name = "One\\Two\\Three.";
String[] str = name.split("\\\\");
for(int i =0; i < str.length ; i++)
System.out.println(str[i]);
works fine.
Explanation
String#split expects a regular expression. The backslash character has a special meaning inside regular expressions, so you need to escape it by using another backslash: \\ Now because the backslash character also has a special meaning inside Java string literals, you have to double each of these again, resulting in "\\\\".
You have missed the quotes
String name = "One\\Two\\Three".
You need to escape it twice:
String name = "One\\Two\\Three."
String[] str = name.split("\\\\");
for(int i =0; i < str.length ; i++)
System.out.println(str[i]);
If you want to test your pattern you should use this tool:
http://www.cis.upenn.edu/~matuszek/General/RegexTester/regex-tester.html
You cant write your test String there and your test Pattern and it can call the methods matches(), lookingAt(), find() and reset(). Also it translates your pattern to Java code (escaping the backslashes and such).
Related
String s = scan.nextLine();
s = s.replaceAll(" ", "");
for (int i = 0; i < s.length(); i++) {
System.out.print(s.charAt(i) + "-");
int temp = s.length();
// this line is the problem
s = s.replaceAll("[s.charAt(i)]", '');
System.out.print((temp - s.length()) + "\n");
i = -1;
}
I was actually using the above method to count each character.
I wanted to use s.charAt(i) inside Regular Expression so that it counts and displays as below. But that line (line 10) doesn't work I know.
If it's possible how can I do it?
Example:
MALAYALAM (input)
M-2
A-4
L-2
Y-1
Java does not have string interpolation, so code written inside a string literal will not be executed; it is just part of the string. You would need to do something like "[" + s.charAt(i) + "]" instead to build the string programmatically.
But this is problematic when the character is a regex special character, for example ^. In this case the character class would be [^], which matches absolutely any character. You could escape regex special characters while building the regex, but this is overly complicated.
Since you just want to replace occurrences an exact substring, it is simpler to use the replace method which does not take a regex. Don't be fooled by the name replace vs. replaceAll; both methods replace all occurrences, the difference is really that replaceAll takes a regex but replace just takes an exact substring. For example:
> "ababa".replace("a", "")
"bb"
> "ababa".replace("a", "c")
"cbcbc"
I've written a program to split a string by |o| and |e| signs.
This is my whole string (which I want to process):
code|e|0.07610 |o| p|e|0.02225 |o| li|e|0.02032 |o| applet|e|0.01305 |o| pre|e|0.01289
I write a utility function to parse the above string, The following is a part of this utility function :
String [] trs = tgs[1].split("[^ |o| ]"); //tgs[1] have the whole string
for (int i=0 ; i<9; i++) {
String t = trs[i].split("[^|e|]")[0];
e.add(new ProbTranslate(t, Double.parseDouble(trs[i].split("[^|e|]")[1])));
}
But it seems to be incorrect (cause I debug the program and then i get incorrect results). I feel that my mistake is in incorrect regex part. So I seek a proper regex for parsing the above string.
Any help would be appreciated. Thanks.
To quote special characters in regular expressions, Java provides a method: java.util.regex.Pattern#quote
Applying to your example above, this could e.g. lead to
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
final String[] split1 = "code|e|0.07610 |o| p|e|0.02225 |o| li|e|0.02032 |o| applet|e|0.01305 |o| pre|e|0.01289".split(Pattern.quote(" |o| "));
for (int i = 0; i < split1.length; ++i) {
final String name = split1[i];
final String[] split2 = name.split(Pattern.quote("|e|"));
for (int j = 0; j < split2.length; ++j) {
System.out.println(split2[j]);
}
System.out.println("");
}
}
}
Output:
code
0.07610
p
0.02225
li
0.02032
applet
0.01305
pre
0.01289
Solution
Make two changes:
"[^ |o| ]" ➔ "( \\|o\\| )"
"[^|e|]" ➔ "(\\|e\\|)"
With those changes, your code would look like this:
String [] trs = tgs[1].split("( \\|o\\| )");
for (int i=0 ; i<9; i++) {
String t = trs[i].split("(\\|e\\|)")[0];
e.add(new ProbTranslate(t, Double.parseDouble(trs[i].split("(\\|e\\|)")[1])));
}
Explanation
There are three problems with your regex.
String#split(String) splits around the subsequences that match the given regex. Therefore, if you want to split around / remove every |o|, then your regex needs to match |o|. However, it appears that you think (incorrectly) that the regex should match everything other than the split subsequence, since you are using a negated character class. Don't do that.
In order to match (or exclude, for that matter) a complete substring in regex, the substring must be contained in parentheses, e.g. (substring). Parentheses denote a capture group. If you use brackets (e.g. [characters]), then it is a character class, which is equivalent to saying "any of these individual characters" rather than "this complete substring".
The character | is a control character in regex. That means that if you want to match a literal | rather than using it to denote regex alternation, then you must escape it. And since this is Java, you must escape the \ too so that Java doesn't try to change \| to some special Java character before the string even gets to the regex engine. Hence, \\|.
I would like to split the special character "\".
However, it doesn't seem to work out using
a.split("\");
or
a.split("\\");
While you could escape the regular expression to String.split with the somewhat surprising
String str = "a\\b\\c";
str.split("\\\\");
it is also possible to compile a Pattern with Pattern.LITERAL and then use Pattern.split(CharSequence) like
String str = "a\\b\\c";
Pattern p = Pattern.compile("\\", Pattern.LITERAL);
String[] arr = p.split(str);
System.out.println(Arrays.toString(arr));
Which outputs
[a, b, c]
This problem is solved by using
a.split("\\\\");
Simply use \n inside the string where need. No method need to split.
"\" special character in java. It is use to skip a character. Such as String s = "I am a \"Student\" of a University!";
Hear Double cote is not allow without using "\".
We can not use "\" single in a string.
String s = "I am a \ Student of a University!";
Hear "\" will make an Err.
No method need to split using "\" Simply use "\n" Where you Need.
Or use another character with it like this
String s = "Thir is a Tiger.\'I like it very nuch!\'I it a pet!";
String s2[] = s.split("\'");
for (int i = 0; i < s2.length; i++) {
System.out.println(i+" value "+s2[i]);
}
String s = "light\\hello\\text.txt";
String s3[] = s.split(Pattern.quote("\\"));
for (int i = 0; i < s3.length; i++) {
System.out.println(i+" value "+s3[i]);
}
Let's say we have a string:
String x = "a| b |c& d^ e|f";
What I want to obtain looks like this:
a
|
b
|
c
&
d
^
e
|
f
How can I achieve this by using x.split(regex)? Namely what is the regex?
I tried this link: How to split a string, but also keep the delimiters?
It gives a very good explanation on how to do it with one delimiter.
However, using and modifying that to fit multiple delimiters (lookahead and lookbehind mechanism) is not that obvious to someone who is not familiar with regex.
The regex for splitsplitting on optional spaces after a word boundary is
\\b\\s*
Note that \\b checks if the preceding character is a letter, or a digit or an underscore, and then matches any number of whitespace characters that the string will be split on.
Here is a sample Java code on IDEONE:
String str = "a| b |c& d^ e|f";
String regex = "\\b\\s*";
String[] spts = str.split(regex);
for(int i =0; i < spts.length && i < 20; i++)
{
System.out.println(spts[i]);
}
I have this unit test:
public void testDeEscapeResponse() {
final String[] inputs = new String[] {"peque\\\\u0f1o", "peque\\u0f1o"};
final String[] expected = new String[] {"peque\\u0f1o", "peque\\u0f1o"};
for (int i = 0; i < inputs.length; i++) {
final String input = inputs[i];
final String actual = QTIResultParser.deEscapeResponse(input);
Assert.assertEquals(
"deEscapeResponse did not work correctly", expected[i], actual);
}
}
I have this method:
static String deEscapeResponse(String str) {
return str.replaceAll("\\\\", "\\");
}
The unit test is failing with this error:
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:686)
at java.util.regex.Matcher.appendReplacement(Matcher.java:703)
at java.util.regex.Matcher.replaceAll(Matcher.java:813)
at java.lang.String.replaceAll(String.java:2189)
at com.acme.MyClass.deEscapeResponse
at com.acme.MyClassTest.testDeEscapeResponse
Why?
Use String.replace which does a literal replacement instead of String.replaceAll which uses regular expressions.
Example:
"peque\\\\u0f1o".replace("\\\\", "\\") // gives peque\u0f1o
String.replaceAll takes a regular expression thus \\\\ is interpreted as the expression \\ which in turn matches a single \. (The replacement string also has special treatment for \ so there's an error there too.)
To make String.replaceAll work as you expect here, you would need to do
"peque\\\\u0f1o".replaceAll("\\\\\\\\", "\\\\")
I think the problem is that you're using replaceAll() instead of replace(). replaceAll expects a regular expression in the first field and you're just trying to string match.
See javadoc for Matcher:
Note that backslashes (\) and dollar
signs ($) in the replacement string
may cause the results to be different
than if it were being treated as a
literal replacement string. Dollar
signs may be treated as references to
captured subsequences as described
above, and backslashes are used to
escape literal characters in the
replacement string.
Thus with replaceAll you cannot replace anything with a backslash. Thus a really crazy workaround for your case would be str.replaceAll("\\\\(\\\\)", "$1")