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 = "(?<!\\\\)[=\"'\\\\]";
Related
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}
This question already has answers here:
Java doesn't work with regex \s, says: invalid escape sequence
(3 answers)
Closed 5 years ago.
I am trying do that but it keeps give me error invalid escape format. I was trying to remove some backslash but didn't work
private string chars = "(lP+" (\\w+)/\\d{3} \\d+ \\w+ \\S+\\s?\"?($|-|[\\w\\\\\.#]+)\"?")";
String should have a capital 'S'
You've missed esacaping a couple of the quotation marks.
You have a run of five backslashes, which should either be 4 or 6.
You probably want something like this instead...
private String chars = "(lP+\" (\\w+)/\\d{3} \\d+ \\w+ \\S+\\s?\"?($|-|[\\w\\\\\\.#]+)\"?\")";
private String chars = "(lP+\" (\\w+)/\\d{3} \\d+ \\w+ \\S+\\s?\"?(\\$|-|[\\w\\\\.#]+)\"?\")";
This question already has an answer here:
Escaping special characters in java regex (not quoting)
(1 answer)
Closed 8 years ago.
I'm trying to define a regex pattern that searches for a caret character, but since ^ is used for negation, I'm not sure how to define the pattern. I'm trying to make the program find a string that is a letter then a caret then a number (as you may have guessed, this is a mathematical term), such as "x^23". This is the line I tried:
String caseFour = "[a-zA-Z]" + "^" + "\\d+";
It's not working. Can anyone help me out?
You need to escape that character also since it is a character of special meaning.
String regex = "[a-zA-Z]\\^\\d+";
This question already has answers here:
Replace special character with an escape preceded special character in Java
(4 answers)
Closed 8 years ago.
In java Regex i want to replace all the special characters to escape sequence.how can i do.
Example ::
//special chars ex "dd[u]i.* " to "dd//[u//]i//.//*"
To escape all special regexp control characters this method can be used:
Matcher.quoteReplacement(String s)
It returns a regular expression that matches exact s.
This comes from the javadoc:
Returns a literal replacement String for the specified String. This method produces a String that will work as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no special meaning.
To split or treat special characters in java pattern as normal one. You have to backshlas it.
\\.; \\* it might be treat now as '.' and '*'.
This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 9 years ago.
I'm trying to split a string at every '.' (period), but periods are a symbol used by java regexes. Example code,
String outstr = "Apis dubli hre. Agro duli demmos,".split(".");
I can't escape the period character, so how else do I get Java to ignore it?
Use "\\." instead. Just using . means 'any character'.
I can't escape the period character, so how else do I get Java to ignore it?
You can escape the period character, but you must first consider how the string is interpreted.
In a Java string (that is fed to Pattern.compile(s))...
"." is a regex meaning any character.
"\." is an illegally-escaped string. This won't compile. As a regex in a text editor, however, this is perfectly legitimate, and means a literal dot.
"\\." is a Java string that, once interpreted, becomes the regular expression \., which is again the escaped (literal) dot.
What you want is
String outstr = "Apis dubli hre. Agro duli demmos,".split("\\.");