This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 8 years ago.
I've written the following code:
String[] arr = ((String) "asd.asd").split(".");
and arr=[]. Why?
split takes a regular expression as an argument. "." in regular means "any character".
Instead, use:
String[] arr = "asd.asd".split("\\.");
The backslashes escape the special meaning of the "." character in a regular expression.
http://docs.oracle.com/javase/tutorial/essential/regex/
split() accepts a regex. you should escape the . use "\\." . In regex . is a special character (Meta character) which means match any character.
You must double escape the ., otherwise the regular expression represents it as "any character".
Also, you don't need to cast "asd.asd" as String.
String[] arr = "asd.asd".split("\\.");
Because '.' is a special character. You need to escape it by writing it like this '\\.'
Related
This question already has answers here:
Groovy/Java split string on parentheses "("
(5 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 3 years ago.
I am trying to split a java string with the character "(".
For example :
split("wer(sde")= "wer"+"sde".
But it give exception. Is there a way to split this string using split() function without changing the character "(" to some other character.
String[] cp=cmd.split("{");
Output:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
The thing is, split() receives as parameter a regular expression. Both {} and () are meta-characters and have a special meaning in a regex, so you need to escape them like this:
String[] cp = cmd.split("\\(|\\)");
The method split of String accept a String, that parameter is a regex :
public String[] split(String regex)
Splits this string around matches of the given regular expression.
Since ( is a reserved character in regex, you need to escape it \(.
But in Java, you need to escape twice \\(, once for the String and the second for the regex
This gives :
s.split("\\(");
Parentheses mean something in RegEx, they're used to group characters together. As such, if you intend to reference the literal character, '(' you must escape it within the RegEx:
String[] cp = cmd.split("\\(");
Note the use of two backslashes. This is because the JVM will also interpret a backslash as a metacharacter for escape purposes, so you must escape the backslash itself with another backslash in order for it to make it into the RegEx.
This question already has answers here:
Replace special character with an escape preceded special character in Java
(4 answers)
Closed 8 years ago.
In java Regex i want to replace all the special characters to escape sequence.how can i do.
Example ::
//special chars ex "dd[u]i.* " to "dd//[u//]i//.//*"
To escape all special regexp control characters this method can be used:
Matcher.quoteReplacement(String s)
It returns a regular expression that matches exact s.
This comes from the javadoc:
Returns a literal replacement String for the specified String. This method produces a String that will work as a literal replacement s in the appendReplacement method of the Matcher class. 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.
To split or treat special characters in java pattern as normal one. You have to backshlas it.
\\.; \\* it might be treat now as '.' and '*'.
This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 9 years ago.
I'm trying to split a string at every '.' (period), but periods are a symbol used by java regexes. Example code,
String outstr = "Apis dubli hre. Agro duli demmos,".split(".");
I can't escape the period character, so how else do I get Java to ignore it?
Use "\\." instead. Just using . means 'any character'.
I can't escape the period character, so how else do I get Java to ignore it?
You can escape the period character, but you must first consider how the string is interpreted.
In a Java string (that is fed to Pattern.compile(s))...
"." is a regex meaning any character.
"\." is an illegally-escaped string. This won't compile. As a regex in a text editor, however, this is perfectly legitimate, and means a literal dot.
"\\." is a Java string that, once interpreted, becomes the regular expression \., which is again the escaped (literal) dot.
What you want is
String outstr = "Apis dubli hre. Agro duli demmos,".split("\\.");
This question already has answers here:
How to replace brackets in strings
(4 answers)
Closed 8 years ago.
I can't seem to replace a string of ":)" to something else, here is my code:
if(message.contains(":)")) message = message.replaceAll(":)", replacement);
This is the error:
Exception in thread "Listen" java.util.regex.PatternSyntaxException: Unmatched closing ')'
near index 0
:)
^
What should I do?
Don't use replaceAll(); use replace() when you want to replace literal strings:
message.replace(":)", replacement)
replaceAll() deals with regular expressions, in which ) has a special meaning, hence the error.
You must escape ) in regexen:
message = message.replaceAll(":\\)", replacement);
This is because ) has special meaning (capture groups), so you have to "tell" regex that you just want a literal ).
Write:
message.replaceAll(Pattern.quote(":)"), replacement);
String#replaceAll accept a regex, not a regular String. ) has a special meaning in regex, using quote will cause treating :) as the String :) and not the regex.
If you don't want to use Pattern#quote, you should escape the ) by \\. Note that escaping a regex is done by \, but in Java, \ is written as \\.
If you don't like any of the mentioned, use String#replace that doesn't accept a regex, and you're fine.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
regular expression for DOT
Say I have a String:
String domain = "www.example.com";
To extract the word "example" I am using the split function in java
String[] keys = domain.split(".");
String result = keys[1];
Clearly this is wrong because the "." is a wrong regular expression since it matches any character.
What is the escape sequence which matches specifically the character "."?
Though this question does seem trivial but I can't seem to find any quick reference or previous answers. Thanks.
By escaping it like as follows
\\.
Use \\.. You need to escape it.
You can get the regular expression for any literal string by using Pattern.quote().
Pattern.quote(".") evaluates to "\\."
In this case it would probably be clearer just to use \\.
You can escape . by prefixing it with \\. Hence, use \\. Reason is that the literal string \\ is a single backslash. In regular expressions, the backslash is also an escape character. The regular expression \\ matches a single backslash.
You can escape the . character by using \\. or using the brackets [.].
Hence your code becomes:
String[] keys = domain.split("\\."); // or domain.split("[.]");
String result = keys[1];
Or you could create a class containing the dot, without escaping:
[.]