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("\\.");
Related
This question already has answers here:
Why does this Java regex cause "illegal escape character" errors?
(7 answers)
Closed 3 years ago.
I want to split a string in java with white spaces. I know that the below line of code does it.
String s[] = str.split("\\\s+");
Here split function takes the regex by which the string must be split. So when I want to split string str through one or more spaces, I should pass \s+ as regex, then why is \\\s+ used?
This will do the split
String s[] = n.split("\\s+");
You don't need a third slash'\' - you get Compile Error.
And first '\' is for escaping the second '\'. Used as an escape character for '\s'.
Like Ismail said, you don't need the third backslash.
In your regex you want to use \s so in Java you also need to escape your backslashes for your tags.
Solution:
Why does this Java regex cause "illegal escape character" errors?
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 an answer here:
Escaping special characters in java regex (not quoting)
(1 answer)
Closed 8 years ago.
I'm trying to define a regex pattern that searches for a caret character, but since ^ is used for negation, I'm not sure how to define the pattern. I'm trying to make the program find a string that is a letter then a caret then a number (as you may have guessed, this is a mathematical term), such as "x^23". This is the line I tried:
String caseFour = "[a-zA-Z]" + "^" + "\\d+";
It's not working. Can anyone help me out?
You need to escape that character also since it is a character of special meaning.
String regex = "[a-zA-Z]\\^\\d+";
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:
Removing a substring between two characters (java)
(3 answers)
Closed 9 years ago.
I want to remove a string that is between two characters and also the characters itself , lets say for example:
i want to replace all the occurrence of the string between "#?" and ";" and remove it with the characters.
From this
"this #?anystring; is #?anystring2jk; test"
To This
"this is test"
how could i do it in java ?
#computerish your answer executes with errors in Java. The modified version works.
myString.replaceAll("#\\?.*?;", "");
The reason being the ? should be escaped by 2 backslashes else the JVM compiler throws a runtime error illegal escape character. You escape ? characters using the backslash .However, the backslash character() is itself a special character, so you need to escape it as well with another backslash.
Use regex:
myString.replaceAll("#\?.*?;", "");
string.replaceAll(start+".*"+end, "")
is the easy starting point. You might have to deal with greediness of the regex operators, however.