How to replace \ followed by letters in java String? - java

I need to delete all tokens that are started with \ and followed by any characters.
I created such a pattern:
input.replaceAll("\\[a-zA-Z0-9]*", "");
But it doesn't work because it doesn't delete \rad from string 5 4\rad.
EDIT:
public static void main(String[] args)
{
String input="Wolf 3 3\4par";
String replaceAll = input.replaceAll("\\\\[a-zA-Z0-9]*", "");
System.out.println("replaceAll=" + replaceAll);
}
Thank you!

The \ is special both in string literals and in regular expressions. To put an actual \ in a regular expression, you have to escape it twice. You also have to assign the result somewhere, which it wasn't clear from your question you were doing. So:
input = input.replaceAll("\\\\[a-zA-Z0-9]*", "");
Complete example: Live Copy
import java.util.*;
public class Temp {
public static void main(String[] args) {
String input = "4 5 \\rad";
input = input.replaceAll("\\\\[a-zA-Z0-9]*", "");
System.out.println(input);
}
}
Output:
4 5

To create \ literal in regex you need to pass \\ to regex engine. But to create \ literal in String you also have to escape it so you need to write it as "\\".
\ literal in regex engine
\\ regex pattern
"\\\\" String representing regex pattern
Now you are using one \ in your regex pattern regex engine sees it as \[ which escapes [ making it simple literal.
Try this way
input.replaceAll("\\\\[a-zA-Z0-9]*", "");

From
Sorry, but my String is exactly 5 4\rad. Indeed how to delete \rad? – Volodymyr Levytskyi
Try
String k= "5 4\rad";
System.out.println(k.replaceAll("\r\\w*", ""));
Output
5 4

Related

how to split string based on delimiter as "\"

I am trying to split the below String based on backslash"\" but unable to achieve this for mar\12\2013
String s1 = "mar\12\2013";
String[] s =s1.split("\\\\");
for(String s2 : s){
System.out.println(s2);
}
In Java "mar\12\2013" is an invalid string.
To use mar\12\2013 in Java, you need "mar\\12\\2013".
If you are taking input from user as "mar\12\2013", then also you have to split it with \\\\.
Reason : .split() takes regex as a parameter.
To specify \ in regex, we need \\, because \ is an escape character in regex.
Also, \ is an escape character in Java, so we need to escape both \ of \\, which makes it \\\\.
String s1= //take input from user // "mar\12\2013"
String[]s=s1.split("\\\\");
for(String s2:s) {
System.out.println(s2);
}
The above code will work the way you wish.
See the working code here.
String s1="mar\12\2013";
String[]s=s1.split("\\");
for(String s2:s){
System.out.println(s2);
}
You doubled up the selectors, i cant actually test atm but i believe it just needs the 2, the first escaping the second.
See the below code it worked fine for me
public class StringTest {
public static void main(String arg[]){
String s1="mar\\12\\2013";
String[]s=s1.split("\\");
for(String s2:s){
System.out.println(s2);
}
}
}

Java backslash split string

I have a string in the format like "test\00216243".
I need to split the string in Java based on the backslash '\' .
My Program:
public class StringTest {
public static void main(String[] args) {
String taskOwner = "test\00216243";
String taskArray[] = taskOwner.split(Pattern.quote(System.getProperty("file.separator")));
System.out.println(taskArray[0]);
}
}
When i run this program i am getting the following result but not the result as 'test'. Any help?
Result:
test16243
Just to add on
\ is a special character in regular expressions, as well as in Java string literals. If you want a literal backslash in a regex, you have to double it twice.
When you type "\\", this is actually a single backslash (due to escaping special characters in Java Strings).
Regular expressions also use backslash as special character, and you need to escape it with another backslash. So in the end, you need to pass "\\" as pattern to match a single backslash.
public static void main(String[] args) {
String taskOwner = "test\\00216243";
String taskArray[] = taskOwner.split("\\\\");
System.out.println(taskArray[0]);
}
output
test
00216243
\002 represents a unicode character. SO i suggest you to split your input according to the character other than alphabet or digit.
String string1 = "test\00216243";
String part[] = string1.split("[^a-z0-9]");
System.out.println(part[0]);
Output:
test
String "test\00216243" should be replaced with "test\\00216243"
Because \ represent the escape sequence, you need to use \\ in the string to represent a \
Try this
public static void main(String[] args) {
String taskOwner = "test\\00216243";
String myarray[] = taskOwner.split("\\\\");
System.out.println(myarray[0]+" "+myarray[1]);
}
Output
test 00216243
Reference:
How to split a java string at backslash

How to replace string: \\\" with an empty string

I want to remove \\\" from a string using java. I have tried with the code mentioned below but I could not get the expected result.
str.replaceAll("\\\"","");
input string:
{\"name\":\"keyword\",\"value\":\"\\\"duck''s\\\"\",\"compareVal\":\"contains\"}
expected string:
{\"name\":\"keyword\",\"value\":\"duck''s\",\"compareVal\":\"contains\"}
Use replace():
str = str.replace("\\\\\"", "");
replaceAl() uses regex for its search term (which would require a more complex string literal), but you don't need regex - your search term is plain text.
Note also that java string literals require each of your search characters to be escaped (by a leading backslash).
str.replace("\\\\\"","");
Explanation:
First \ => escaping a '\'
Second \ => escaping a '\'
\" => escaping '"'
Because \ and " are reserved symbols you have to indicate you want to treat them as the symbol they are by escaping with \ before.
public static void main(String s[])
{
String inputString = "\\\"name\\\"";
String outputString = inputString.replace("\\", "").replace("\"","");
System.out.println("Output string is as following :" + outputString);
}

Regular expression for password at least 8 characters with alphanumeric/special characters

My test method is here and all the other inputs are fine but the problem is can't test on the string containing \ backslash character,
public static void main(String[] args) {
String reg2 = "^[a-zA-Z0-9#\\#$%&*()_+\\]\\[';:?.,!^-]{8,}$";
System.out.println("".matches(reg2));
System.out.println("Pass".matches(reg2));
System.out.println("P#ssw0r\\".matches(reg2)); //not work
System.out.println("P#ssw0r$".matches(reg2));
System.out.println("P#ssw0r(".matches(reg2));
System.out.println("P#ssw0r)".matches(reg2));
System.out.println("P#ssw0r_".matches(reg2));
System.out.println("P#ssw0r+".matches(reg2));
System.out.println("P#ssw0r'".matches(reg2));
System.out.println("P#ssw0r;".matches(reg2));
System.out.println("P#ssw0r:".matches(reg2));
System.out.println("P#ssw0r?".matches(reg2));
System.out.println("P#ssw0r.".matches(reg2));
System.out.println("P#ssw0r.][".matches(reg2));
}
Please suggest my regular expression to work on the string with \ character, and guide me if my approach is wrong.
Escapse \ character twice (one for java and one for regex)
String reg2 = "^[a-zA-Z0-9#\\\\#$%&*()_+\\]\\[';:?.,!^-]{8,}$";

Splitting a space separated string

String numbers = "5 1 5 1";
So, is it:
String [] splitNumbers = numbers.split();
or:
String [] splitNumbers = numbers.split("\s+");
Looking for: ["5","1","5","1"]
Any idea why neither of the .split lines will work? I tried reading answers about the regex, but I'm not getting anywhere.
In your above case split("\s+");, you need to escape \ with another backslash, which is:
split("\\s+");
Or
split(" "); also can do it
Note that split("\\s+"); split any length of whitespace including newline(\n), tab(\t) while split(" "); will split only single space.
For example, when you have string separated with two spaces, say "5 1 5 1" ,
using split("\\s+"); you will get {"5","1","5","1"}
while using split(" "); you will get {"5","","1","5","1"}
You must escape the regex with an additional \ since \ denotes the escape character:
public static void main(String[] args) {
String numbers = "5 1 5 1";
String[] tokens = numbers.split("\\s+");
for(String token:tokens){
System.out.println(token);
}
}
So the additional \ escapes the next \ which is then treated as the literal \.
When using \\s+ the String will be split on multiple whitespace characters (space, tab, etc).

Categories