This question already has an answer here:
using parenthesis replaceAll method Java
(1 answer)
Closed 7 years ago.
In the code below I'm trying to replace in text occurrences of fromString withtoString, but no replacement takes place. How to set the parenthesis in the regex to make this work?
public static void main(String[] args) {
String fromString = "aaa(bbb)";
String toString = "X";
String text = "aaa(bbb) aaa(bbb)";
String resultString = text.replaceAll(fromString, toString);
System.out.println(resultString);
}
replaceAll uses regex as its first argument. Parenthesis () are used for capturing groups so need to be escaped
String fromString = "aaa\\(bbb\\)";
Since you can't modify the input String you can use Pattern.quote
String resultString = text.replaceAll(Pattern.quote(fromString), toString);
or simply String.replace could be used which doesnt use regex arguments
String resultString = text.replace(fromString, toString);
Related
This question already has answers here:
How do I split a string in Java?
(39 answers)
Split string with dot as delimiter
(13 answers)
Closed 2 years ago.
I have a string, which contains ^ symbol given below.
String tempName = "afds^afcu^e200f.pdf"
I want to split it like below
[afds, afcu, e200f]
How to resolve this.
The parameter to split() is a regular expression, which has special meta-characters. If the delimiter you're splitting on contains those special characters (e.g. ^), you have two options:
Escape the characters using \, which has to be doubled in a Java string literal to \\:
String[] result = tempName.split("\\^");
If you don't want to bother with that, or if the delimiter is dynamically assigned at runtime, so you can't escape the special characters yourself, call Pattern.quote() to do it for you:
String[] result = tempName.split(Pattern.quote("^"));
you need to add \\ in split method of String to split the string by this (^), because ^ is an special character in regular expression and you need to omit it with \\:
String tempName = "afds^afcu^e200f.pdf";
String [] result = tempName.split("\\^");
System.out.println(Arrays.toString(result));
Java characters that have to be escaped in regular expressions are:
.[]{}()<>*+-=!?^$|
Two of the closing brackets (] and }) are only need to be escaped after opening the same type of bracket.
In []-brackets some characters (like + and -) do sometimes work without escape.
more info...
String.split() in Java takes a regular expression. Since ^ is a control character in regex (when at the beginning of the regex string it means "the start of the line"), we need to escape it with a backslash. Since backslash is a control character in Java string literals, we also need to escape that with another backslash.
String tempName = "afds^afcu^e200f.pdf";
String[] parts = tempName.split("\\^");
You can use the retrieve a substring without the file extension and split that according to the delimiter that is required (^). This is shown below:
public static void main(String[] args) {
String tempName = "afds^afcu^e200f.pdf";
String withoutFileFormat = tempName.substring(0, tempName.length() - 4); //retrieve the string without the file format
String[] splitArray = withoutFileFormat.split("\\^"); //split it using the "^", use escape characters
System.out.println(Arrays.toString(splitArray)); //output the result
}
Required Output:
[afds, afcu, e200f]
This question already has answers here:
How to replace a String in java which contains dot?
(3 answers)
Closed 3 years ago.
In my program I want to remove all the dots(.) I've tried to do this but it's not working.
public class MyClass {
public static void main(String args[]) {
String str = " .Hello.World..Awesome!. ";
System.out.println(str.replaceAll(".",""));
}
}
Replace all takes a regex as the first argument and you need to escape "." like
"\\."
str.replaceAll("\\.","")
This is not working because . is a special regex character. You must escape this using a backslash. The dot is used as a capture all in regex.
System.out.println(str.replaceAll("\\.",""));
You can read up on all of the special characters in regex here - https://www.regular-expressions.info/characters.html
This question already has an answer here:
Divide/split a string on quotation marks
(1 answer)
Closed 8 years ago.
This question is Pretty Simple
How to Split String With double quotes in java?,
For example I am having string Do this at "2014-09-16 05:40:00.0",After Splitting, I want String like
Do this at
2014-09-16 05:40:00.0,
Any help how to achieve this?
This way you can escape inner double quotes.
String str = "Do this at \"2014-09-16 05:40:00.0\"";
String []splitterString=str.split("\"");
for (String s : splitterString) {
System.out.println(s);
}
Output
Do this at
2014-09-16 05:40:00.0
Use method String.split()
It returns an array of String, splitted by the character you specified.
public static void main(String[] args) {
String test = "Do this at \"2014-09-16 05:40:00.0\"";
String parts[] = test.split("\"");
String part0 = parts[0];
String part1 = parts[1];
System.out.println(part0);
System.out.println(part1);
}
output
Do this at
2014-09-16 05:40:00.0
Try this code. Maybe it can help
String str = "\"2014-09-16 05:40:00.0\"";
String[] splitted = str.split("\"");
System.out.println(splitted[1]);
The solutions provided thus far simply split the string based on any occurrence of double-quotes in the string. I offer a more advanced regex-based solution that splits only on the first double-quote that precedes a string of characters contained in double quotes:
String[] splitStrings =
"Do this at \"2014-09-16 05:40:00.0\"".split("(?=\"[^\"].*\")");
After this call, split[0] contains "Do this at " and split[1] contains "\"2014-09-16 05:40:00.0\"". I know you don't want the quotes around the second string, but they're easy to remove using substring.
This question already has answers here:
Regex for splitting a string using space when not surrounded by single or double quotes
(16 answers)
Closed 8 years ago.
I have a string,
String string = "sdeLING9497896,\"kifssd9497777 999_13\",Kfsheis9497896e,ersdG9497896,aseLING9497896 erunk15426 \nEsea4521
\"\nSdfes451 45264\" \"kiseliog949775 959_13\"";
I may have spaces, comma, tab or new line character in this string. My requirement is to split this string using space,comma,tab and new line but the spaces inside the double quoted strings should be excluded.
My sample code:
public class RegExTest {
public static void main(String[] args) {
Set<String> idSet = new HashSet<String>();
String string = "sdeLING9497896,\"kipliog9497777 999_13\",KIPLING9497896e,ersdG9497896,aseLING9497896 erunk15426 \nEsea4521 \"\nSdfes451 45264\"";
String ids[] = string.split(**"NEED A REGEX HERE"**);
for (String id : ids) {
if (id.trim().length() > 0) {
idSet.add(id);
}
}
System.out.println(idSet);
}
}
My expectation is: sdeLING9497896, kifssd9497777 999_13, KIPLING9497896e, ersdG9497896, aseLING9497896, erunk15426, Esea4521, Sdfes451 45264, kiseliog949775 959_13
Please guide me to solve this!
You can use this regex:
String[] tok = string.split("(?=((\\S*\\s){2})*\\S*$)[\\s,]+");
This will split on \s or , only if \s is outside double quotes by making sure there are even number of quotes after delimiter \s.
This question already has answers here:
The split() method in Java does not work on a dot (.) [duplicate]
(7 answers)
Closed 9 years ago.
My problem is that the array ms[ ] doesn't get values when I do split( );
Why is this happening ?
public class Test {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss."); //change format
String msgTime = ft.format(date);
System.out.println(msgTime);
String ms[] = msgTime.split(".");
System.out.println(ms.length);
}
}
The problem is split() function takes regular expression as argument, not a simple string. And "." regular expression means "any symbol".
So you need just escape it.
String ms[] = msgTime.split("\\.");
I'm guessing you meant to do
String ms[] = msgTime.split("\\.");
String.split() takes a regular expression so you should escape any special characters, such as ..