String str ="|m4oho5kspqikkfn2may72osnfzmn3gutwzqctblzqy6rygwzxbra6bjkmy|113|70|";
String[] tokens = str.split("|");
System.out.println(tokens[0]);
System.out.println(tokens[1]);
result in white::
0
I just need this
But the only thing I want to come back is: m4oho5kspqikkfn2may72osnfzmn3gutwzqctblzqy6rygwzxbra6bjkmy
Sorry not be much English I am using Google Translator
| is a regex protected character. You need to escape it when splitting like so:
str.split("\\|");
Regards
You can find more detailed information for regex expression in the java doc:
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
There are a lot of really good examples. I strongly recommend to check them.
For literals you must use double backslash (\) to escape the escaped character.
Cheers.
Related
I have long string looking like this: \c53\e59\c9\e28\c20140326\a4095\c8\c15\a546\c11 and I need to find expressions starting with \a and followed by digits. For example: \a574322
And I have no idea how to build it. I can't use:
Pattern p = Pattern.compile("\\a\\d*");
because \a is special character in regex.
When I try to group it like this:
Pattern p = Pattern.compile("(\\)(a)(\\d)*");
I get unclosed group error even though there is even number of brackets.
Can you help me with this?
Thank you all very much for solution.
You can use this regex:
\\\\a\\d+
Code Demo
Since in Java you need to double escape the \\ once for String and second time for regex engine.
You have to change your regex to:
Pattern p = Pattern.compile("(\\\\a\\d+)");
The regex is:
(\\a\d+)
The idea is to escape a backslash and then also escape the backslash for \a, and match digits too.
You need 4 \.
2 to indicate to regex that it is not a special character, but a plain \, and 2 for each to tell the Java String that these are not special characters either. So you need to represent it in code this way:
"\\\\a\\d*"
Which is actually the regex \\a\d*
\\(a)[0-9]+ this should work
you can't try your regexps on this page or some similar
http://regex101.com/
I need to escape all quotes (') in a string, so it becomes \'
I've tried using replaceAll, but it doesn't do anything. For some reason I can't get the regex to work.
I'm trying with
String s = "You'll be totally awesome, I'm really terrible";
String shouldBecome = "You\'ll be totally awesome, I\'m really terrible";
s = s.replaceAll("'","\\'"); // Doesn't do anything
s = s.replaceAll("\'","\\'"); // Doesn't do anything
s = s.replaceAll("\\'","\\'"); // Doesn't do anything
I'm really stuck here, hope somebody can help me here.
Thanks,
Iwan
You have to first escape the backslash because it's a literal (yielding \\), and then escape it again because of the regular expression (yielding \\\\). So, Try:
s.replaceAll("'", "\\\\'");
output:
You\'ll be totally awesome, I\'m really terrible
Use replace()
s = s.replace("'", "\\'");
output:
You\'ll be totally awesome, I\'m really terrible
Let's take a tour of String#repalceAll(String regex, String replacement)
You will see that:
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)
So lets take a look at Matcher.html#replaceAll(java.lang.String) documentation
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.
You can see that in replacement we have special character $ which can be used as reference to captured group like
System.out.println("aHellob,aWorldb".replaceAll("a(\\w+?)b", "$1"));
// result Hello,World
But sometimes we don't want $ to be such special because we want to use it as simple dollar character, so we need a way to escape it.
And here comes \, because since it is used to escape metacharacters in regex, Strings and probably in other places it is good convention to use it here to escape $.
So now \ is also metacharacter in replacing part, so if you want to make it simple \ literal in replacement you need to escape it somehow. And guess what? You escape it the same way as you escape it in regex or String. You just need to place another \ before one you escaping.
So if you want to create \ in replacement part you need to add another \ before it. But remember that to write \ literal in String you need to write it as "\\" so to create two \\ in replacement you need to write it as "\\\\".
So try
s = s.replaceAll("'", "\\\\'");
Or even better
to reduce explicit escaping in replacement part (and also in regex part - forgot to mentioned that earlier) just use replace instead replaceAll which adds regex escaping for us
s = s.replace("'", "\\'");
This doesn't say how to "fix" the problem - that's already been done in other answers; it exists to draw out the details and applicable documentation references.
When using String.replaceAll or any of the applicable Matcher replacers, pay attention to the replacement string and how it is handled:
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.
As pointed out by isnot2bad in a comment, Matcher.quoteReplacement may be useful here:
Returns a literal replacement String for the specified String. .. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes (\) and dollar signs ($) will be given no special meaning.
You could also try using something like StringEscapeUtils to make your life even easier: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html
s = StringEscapeUtils.escapeJava(s);
You can use apache's commons-text library (instead of commons-lang):
Example code:
org.apache.commons.text.StringEscapeUtils.escapeJava(escapedString);
Dependency:
compile 'org.apache.commons:commons-text:1.8'
OR
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.8</version>
</dependency>
i am trying very simple splitting.
I dont know why it is not working.
String abc= "192.168.120.2";
String[] eachByteabc= abc.split(".");
When I debug it and see, I get the result that abc contains : 192.168.120.2.
But when I do split, it does not give me error but gives me null result.
I think, i have made some silly mistake.
Can you tell me where I am wrong. What should I do.
Thank you in advance.
Try it =):
String[] eachByteabc= abc.split("\\.");
You need to escape the ., since it's a regex operator. Change it to:
String[] eachByteabc= abc.split("[.]");
Addition, thanks to #sparks:
While this will work, the [] characters in regex are used to annotate
a set, so if you are looking for where it might be in a limited series
of characters, you should use them.
In this case - use \\. to escape the . character.
public String[] split(String regex) takes a regular expression as an argument. You must escape the point, since it's a regex operator.
String[] eachByteabc = abc.split("."); is not eorr,but you can not to debug and Watch the values.use String[] eachByteabc = abc.split(".");you can Watch values in the debug.
String.split uses regex, so you need to use abc.split("\\.");
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29
I am having some problem with searching for a special character "(".
I got a java.util.regex.PatternSyntaxException exception has occurred.
It might have something to do with "(" being treated as special character.
I am not very good with pattern expression. Can someone help me properly search for the escape character?
// I need to split the string at the "("
String myString = "Room Temperature (C)";
String splitList[] = myString.split ("("); // i got an exception
// I tried this but got compile error
String splitList[] = myString.split ("\(");
Try one of these:
string.split("\\(");
string.split(Pattern.quote("("));
Since a string split takes a regular expression as an argument, you need to escape characters properly. See Jon Skeet's answer on this here:
The reason you got an exception the first time is because split() takes a regular expression as argument, and ( has a special meaning there, as you suggest. To avoid this, you need to escape it using a \, like you tried.
What you missed, is that you also need to escape your backslashes with an extra \ in Java, meaning you need a total of two:
String splitList[] = myString.split ("\\(");
You need to escape the character via backslashes: string.split("\\(");
( is one of regex special characters. To escape it you can use e.g.
split(Pattern.quote("(")),
split("\\Q(\\E"),
split("\\("),
split("[(]").
What's the correct regex for a plus character (+) as the first argument (i.e. the string to replace) to Java's replaceAll method in the String class? I can't get the syntax right.
You need to escape the + for the regular expression, using \.
However, Java uses a String parameter to construct regular expressions, which uses \ for its own escape sequences. So you have to escape the \ itself:
"\\+"
when in doubt, let java do the work for you:
myStr.replaceAll(Pattern.quote("+"), replaceStr);
You'll need to escape the + with a \ and because \ is itself a special character in Java strings you'll need to escape it with another \.
So your regex string will be defined as "\\+" in Java code.
I.e. this example:
String test = "ABCD+EFGH";
test = test.replaceAll("\\+", "-");
System.out.println(test);
Others have already stated the correct method of:
Escaping the + as \\+
Using the Pattern.quote method which escapes all the regex meta-characters.
Another method that you can use is to put the + in a character class. Many of the regex meta characters (., *, + among many others) are treated literally in the character class.
So you can also do:
orgStr.replaceAll("[+]",replaceStr);
Ideone Link
If you want a simple string find-and-replace (i.e. you don't need regex), it may be simpler to use the StringUtils from Apache Commons, which would allow you to write:
mystr = StringUtils.replace(mystr, "+", "plus");
Say you want to replace - with \\\-, use:
text.replaceAll("-", "\\\\\\\\-");
String str="Hello+Hello";
str=str.replaceAll("\\+","-");
System.out.println(str);
OR
String str="Hello+Hello";
str=str.replace(Pattern.quote(str),"_");
System.out.println(str);
How about replacing multiple ‘+’ with an undefined amount of repeats?
Example: test+test+test+1234
(+) or [+] seem to pick on a single literal character but on repeats.