Split() in java [duplicate] - java

This question already has answers here:
How to split a java string at backslash
(8 answers)
Closed 4 years ago.
How to split string on the basis on escape sequence() in java.
Like :
String a ="23\\45\\20";
String b[]=a.split("\\");//this doesn't work
What is best way to do this.

Due to \ is escape character in both Java and Regex Expression, so you just need to use \\\\ instead of \\
String b[]=a.split("\\\\");
\\\\ in java will convert to \\
\\ in regex will convert to \

There are no backslash characters in this string:
String a ="23\45\20";
That's because \45 and \20 are seen as octal escape sequences, which expand to % and an unprintable \x16 character respectively.
Escape these backslashes to get the desired effect:
String a ="23\\45\\20";

Related

How to replace "\" with "/" in java? [duplicate]

This question already has answers here:
Java replaceAll: Cannot replace string with backslash
(3 answers)
What are all the escape characters?
(5 answers)
Closed 11 months ago.
I have a string like s = "abc\def" and I want to replace "" with "/" and makes the string like "abc/def/".
I tried replaceAll("\\","/") but the compiler is giving error for string
error: illegal escape character
String s="abc\def";
The issue is that Java uses \ as the string escape character, but also as the regex escape character, and replaceAll performs a regex search. So you need to doubly escape the backslash (once to make it a literal \ in the string, and once to make it a literal character in the regular expression):
result = str.replaceAll("\\\\", "/");
Alternatively, you don’t actually need regular expressions here, so the following works as well:
result = str.replace("\\", "/");
Or, indeed, using the single-char replacement version:
result = str.replace('\\', '/');

Using Regex for splitting a string in java [duplicate]

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?

How find \ in java regex? [duplicate]

This question already has answers here:
Regular expression to match a backslash followed by a quote
(3 answers)
Closed 4 years ago.
I want to find ????\??-?? string in D:\it\2020\02-20\123\13.json. ? is a digit.
How Can I find it with java regex?
When writing a regex in java, we escape the \ character with another backslash. So if you want to put one backslash character then you should do \\ but if you want to escape the backslash then you do it with \\\\. Following regex should work fine in your case:
\\d+\\\\\\d+-\\d+
If you want to be specific then use:
\\d{4}\\\\\\d{2}-\\d{2}
\d+\\\d+-\d+
\d{4}\\\d{2}-\d{2}

Replace backslash with double backslash [duplicate]

This question already has answers here:
String.replaceAll single backslashes with double backslashes
(5 answers)
Closed 6 years ago.
I want to change the backslash in a string to double backslash.
I have
String path = "C:\Program Files\Text.txt";
and I want to change it to
"C:\\Program Files\\Text.txt"
replaceAll is using regex, and since you don't need to use regex here simply use
path = path.replace("\\", "\\\\");
\ is special in String literals. For instance it can be used to
create special characters like tab \t, line separators \n \r,
or to write characters using notation like \uXXXX (where X is hexadecimal value and XXXX represents position of character in Unicode Table).
To escape it (and create \ character) we need to add another \ before it.
So String literal representing \ character looks like "\\". String representing two \ characters looks like "\\\\".
Using String#replace()
String s= "C:\\Program Files\\Text.text";
System.out.println(s.replace("\\", "\\\\"));

How do you match military time? [duplicate]

This question already has answers here:
Invalid escape sequence \d
(2 answers)
Closed 10 years ago.
I'm trying to create a valid Java regex for matching strings representing standard "military time":
String militaryTimeRegex = "^([01]\d|2[0-3]):?([0-5]\d)$";
This gives me a compiler error:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )
Where am I going wrong?!?
Make sure you use double backslashes for escaping characters:
String militaryTimeRegex = "^([01]\\d|2[0-3]):?([0-5]\\d)$";
Single backslashes indicate the beginning of an escape sequence. You need to use \\ to get the character as it appears in the String.
To answer your comment, you are currently only matching 19:00. You need to account for the additional :00 at the end of the String in your pattern:
String militaryTimeRegex = "^([01]\\d|2[0-3]):?([0-5]\\d):?([0-5]\\d)$";
In Java, you need to double-escape all the \ characters:
String militaryTimeRegex = "^([01]\\d|2[0-3]):([0-5]\\d):([0-5]\\d)$";
Why? because \ is the escape character for strings, and if you need a literal \ to appear somewhere inside a string, then you have to escape it, too: \\.
According to the error message \d does not exist. Escape it with \\d
Although \d is valid regex syntax, you need to escape the backslash in the Java string:
String militaryTimeRegex = "^([01]\\d|2[0-3]):?([0-5]\\d)$";

Categories

Resources