This question already has an answer here:
regex: How to escape backslashes and special characters?
(1 answer)
Closed 8 years ago.
What's the difference between replaceAll("\\s+") and replaceAll("\\\\s+")? Usually I use \\s+ but sometimes I see \\\\s+.
\\s+ --> replaces 1 or more spaces.
\\\\s+ --> replaces the literal \ followed by s one or more times.
Code:
public static void main(String[] args) {
String s = "\\sbas def";
System.out.println(s);
System.out.println(s.replaceAll("\\s+", ""));
System.out.println(s.replaceAll("\\\\s+", ""));
}
O/P :
\sbas def
\sbasdef
bas def
Related
This question already has answers here:
Find and replace all NewLine or BreakLine characters with \n in a String - Platform independent
(3 answers)
Closed 2 years ago.
I have the following code, which parses and replace whenever it finds a pattern in the string.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class HelloWorld {
public static void main(String []args){
String str = "nv13=4t543r523w4r2w3e4r\n8nv13=4t543r523w4r2w3e4r\n8nv13=4t543r523w4r2w3e4r\n8nv13=4t543r523w4r2w3e4r\n8nv13=4t543r523w4r2w3e4r\n8nv13=4t543r523w4r2w3e4r\n5";
String newstr = str.replaceAll("(?<=nv13=)(.*\n?)(?=\n8)", "hello");
System.out.println(newstr);
}
}
I expect an output of
nv13=hello\n8nv13=hello\8nv13=hello\n8nv13=hello8nv13=hello\n8nv13=4t543r523w4r2w3e4r\n5
but,instead, I get
nv13=hello
8nv13=hello
8nv13=hello
8nv13=hello
8nv13=hello
8nv13=4t543r523w4r2w3e4r
5
What am I missing the above code to fix the \n , to actually print it out in the string without going to the next line ?
newstr = newstr.replace("\n", "\\n");
You can escape the \ like this \\. So this will print \n and not actually go on a new line.
Output:
nv13=hello\n8nv13=hello\n8nv13=hello\n8nv13=hello\n8nv13=hello\n8nv13=4t543r523w4r2w3e4r\n5
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 answers here:
Regex to match only commas not in parentheses?
(3 answers)
Closed 3 years ago.
I've the string looking like this:
word1-word2-word3-\d{1,2}\w?-\d{1,2}\w?|word1-word2-\d{1,2}\w?-\d{1,2}\w?|word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w
I'd like to split this string by '|' everywhere where it doesn't precedes by '('. So result should be:
["word1-word2-word3-\d{1,2}\w?-\d{1,2}\w?", "word1-word2-\d{1,2}\w?-\d{1,2}\w?", "word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w"]
I've trying to use negative lookahead \((?!\|) which split the text to on '('.
UPDATE
So I want to achieve not splitting the "word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w" on '|' where that character is precedes by '('.
Could someone please help me with this?
Code:
public static void main(String[] args) {
String str = "word1-word2-word3-\\d{1,2}\\w?-\\d{1,2}\\w?|word1-word2-\\d{1,2}\\w?-\\d{1,2}\\w?|word1-word2-word3-(\\d{1,2}\\w?\\d{1,2}|\\d{1,2}\\w?)-\\d{1,2}\\w";
String[] arrOfStr = str.split("\\|");
for (String a : arrOfStr)
System.out.println(a);
}
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I was expecting the OUTPUT to be 3 from the below class:
public class Pattern {
public static void main(String[] args) {
String data = "111,577,5099,541,142,
2015-08-01 00:08:42,2015-08-01 06:31:52|
674,898,7061,36,105,2015-08-01 19:28:45,
2015-08-02 14:46:27|948,522,1840,66,889,
2015-08-02 13:04:56,2015-08-02 19:39:57";
if(data.contains("|"))
{
String pattern[] = data.split("|");
System.out.println("the pattern length: "+pattern.length);
}
}
}
OUTPUT:
the pattern length: 180
The character | is a special character in regexes (the split method uses regexes)
You'll have to go with
String pattern[] = data.split("\\|");
kindly update your code likewise,
String pattern[] = data.split("\\|");
This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 8 years ago.
This is probably a very easy question but I'm going to give you the code first.
import java.util.Scanner;
public class help {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Give: ");
String s = sc.next();
String[] parts = s.split(".");
System.out.println(parts.length);
}
}
Even if I give 192.168.1.1.1.1.1 or 1.2.3 or ... the parts.length will always be 0, can somebody please explain to me why and how I can let it be 4 if i enter 1.2.3.4?
You need s.split("\\.") because the argument to split is a regular expression. The . character in a regular expression means "any character", and you need to escape it with the backslash to have it mean "dot".
Because "." is a special character, meaning "any character".
You need to escape it to be able to use it as the character ".":
String[] parts = s.split("\\.");