How find \ in java regex? [duplicate] - java

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}

Related

how To change my regex to reject underscores [duplicate]

This question already has answers here:
Regular Expressions: How to Express \w Without Underscore
(7 answers)
Closed 2 years ago.
Currently have this regex string in my java code:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It successfully accepts those characters, however it also accepts underscore, how can modify the regex to reject underscore appearing anywhere in the string?
Your regex is:
^[\\w\\-\\ \\#\\.\\/]{0,70}$
It is using \w which is equivalent of [a-zA-Z0-9_], hence it allows underscore also.
You can change your character class to this:
^[-#. a-zA-Z0-9\\/]{0,70}$
Note that space, dot, #, / don't need to be escaped inside [...] and - if placed at first or last position doesn't require escaping either.

Split() in java [duplicate]

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";

How to split by escape character in java? [duplicate]

This question already has answers here:
How to split a java string at backslash
(8 answers)
Closed 7 years ago.
I have the following string:
c:\Users\moises\file
and I'm trying to use:
String fileName = path.split("\\")[3];
but I get this error:
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
How can I split the string with the "\" character
path.split("\\\\");
You need to escape your escapes
You'll need four backslashes in a row.
The regular expression with then consist of two backslashes: that means a literal backslash.
I think, it is because you need to escape four times, try this:
path.split("\\\\");

Require character escapes regex [duplicate]

This question already has answers here:
Match key-value pattern regex
(2 answers)
Closed 8 years ago.
I am attempting to build a key-value serializer and I need to match all characters except =, ", , and \ when not prefixed with a backslash.
So far what I have to match the rest of the pattern is with 'here' being where I need to match the 'special' characters ^[[a-zA-Z0-9-]+[=][^here]]+$
This pattern matches all such characters:
(?<!\\)[="'\\]
In java, the string literal is ugly with all the escapes:
String regex = "(?<!\\\\)[=\"'\\\\]";

Replace/remove String between two character [duplicate]

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.

Categories

Resources