negate and replacing string with regex in java backslash and quate - java

I would like to use regex in order to replace '"' value but only if this char isnt followed by backslash
e.g.
\"Hello\" -> \"Hello\"
"Hello" -> 'Hello'
i used the following statement but in deletes the previous char of the '"'
[^\\])\"
how do i do that in java?

Use a negative lookbehind assertion.
string.replaceAll("(?<!\\\\)\"", "'");
In java regex \\\\ matches a single backslash. So (?<!\\\\)\" matches the double quotes only if it isn't preceded by a backslash.
String s1 = "\\\"Hello\\\"";
String s2 = "\"Hello\"";
System.out.println(s1.replaceAll("(?<!\\\\)\"", "'"));
System.out.println(s2.replaceAll("(?<!\\\\)\"", "'"));
Output:
\"Hello\"
'Hello'

Related

Matching a whole word with leading or trailing special symbols like dollar in a string

I can replace dollar signs by using Matcher.quoteReplacement. I can replace words by adding boundary characters:
from = "\\b" + from + "\\b";
outString = line.replaceAll(from, to);
But I can't seem to combine them to replace words with dollar signs.
Here's an example. I am trying to replace "$temp4" (NOT $temp40) with "register1".
String line = "add, $temp4, $temp40, 42";
String to = "register1";
String from = "$temp4";
String outString;
from = Matcher.quoteReplacement(from);
from = "\\b" + from + "\\b"; //do whole word replacement
outString = line.replaceAll(from, to);
System.out.println(outString);
Outputs
"add, $temp4, $temp40, 42"
How do I get it to replace $temp4 and only $temp4?
Use unambiguous word boundaries, (?<!\w) and (?!\w), instead of \b that are context dependent:
from = "(?<!\\w)" + Pattern.quote(from) + "(?!\\w)";
See the regex demo.
The (?<!\w) is a negative lookbehind that fails the match if there is a non-word char immediately to the left of the current location and (?!\w) is a negative lookahead that fails the match if there is a non-word char immediately to the right of the current location. The Pattern.quote(from) is necessary to escape any special chars in the from variable.
See the Java demo:
String line = "add, $temp4, $temp40, 42";
String to = "register1";
String from = "$temp4";
String outString;
from = "(?<!\\w)" + Pattern.quote(from) + "(?!\\w)";
outString = line.replaceAll(from, to);
System.out.println(outString);
// => add, register1, $temp40, 42
Matcher.quoteReplacement() is for the replacement string (to), not the regex (from). To include a string literal in the regex, use Pattern.quote():
from = Pattern.quote(from);
$ has special meaning in regex (it means “end of input”). To remove any special meaning from characters in your target, wrap it in regex quote/unquote expressions \Q...\E. Also, because $ is not ”word” character, the word boundary won’t wiork, so use look arounds instead:
line = line.replaceAll("(?<!\\S)\\Q" + from + "\\E(?![^ ,])", to);
Normally, Pattern.quote is the way to go to escape characters that may be specially interpreted by the regex engine.
However, the regular expression is still incorrect, because there is no word boundary before the $ in line; space and $ are both non-word characters. You need to place the word boundary after the $ character. There is no need for Pattern.quote here, because you're escaping things yourself.
String from = "\\$\\btemp4\\b";
Or more simply, because you know there is a word boundary between $ and temp4 already:
String from = "\\$temp4\\b";
The from variable can be constructed from the expression to replace. If from has "$temp4", then you can escape the dollar sign and add a word boundary.
from = "\\" + from + "\\b";
Output:
add, register1, $temp40, 42

Java back slash character not being output in string

I'm trying to replace some text in a file and the string contains a file path which requires some back slashes, normally using "\" works fine and produces a single \ on the output but my current code is not outputting any backslashes
String newConfig = readOld().replaceAll(readOld(),"[HKEY_CURRENT_USER\\Software\\xxxx\\xxxx\\Config]");
The "\" starts an escape sequence,
A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.
So, (ludicrously perhaps)
String old = readOld();
String newConfig = old.replaceAll(old,
"[HKEY_CURRENT_USER\\\\Software\\\\xxxx\\\\xxxx\\\\Config]");
Or,
String old = readOld();
char backSlash = '\\';
String newConfig = old.replaceAll(old,
"[HKEY_CURRENT_USER" + backSlash + backSlash + "Software"
+ backSlash + backSlash + "xxxx"
+ backSlash + backSlash + "xxxx"
+ backSlash + backSlash + "Config]");
You should use replace here as it may possible your readOld() method may be having some special characters (i.e +,*,. etc.) which are reserved in regExp so better to use replace.(As replaceAll may throw Exception for invalid regular Expression)
String newConfig = readOld().replace(readOld(),"replacement");
As here it seems you are replacing whole String why not just assign String directly to newConfig
From JavaDoc for replaceAll
Backslashes (\) and dollar signs ($) in the replacement
string may cause the results to be different than if it were being
treated as a literal replacement String
So either go For \\\\ (As suggested by Elliott Frinch) in String or use replace.

split string based on comma delimiter

what is wrong in the following code?
String selectedCountriesStr = countries.replaceAll("[", "").replaceAll("]", "").trim();
String[] selectedCountriesArr = selectedCountriesStr.split(",");
Input String [10000,20000,304050,766666]
Getting error java.util.regex.PatternSyntaxException: Unclosed character class near index 0
You have to escape square brackets because replaceAll() interprets the first argument as a regular expression:
replaceAll("\\[", "")
^^
because, as the error message tells you, the are used for character classes in a regex. Double backslashes are necessary, because "\[" would be an invalid escape sequence. Since the backslash is escaped, the regex engine only receives one backslash.
Also, you can use
replace("[", "")
it will also replace all occurrences of the given CharSequence as is.
You can read more about it in JavaDoc.
Brackets are regex metacharacters, you need to prefix them with a backslash:
.replaceAll("\\[", "").replaceAll("\\]", "")
Also, since this is a simple string substitution, you'd better use .replace():
.replace("[", "").replace("]", "")
String str = "hi,hello,abc,example,problems";
String[] splits = str.split(",");
System.out.println("splits.size: " + splits.length);
for(String asset: splits){
System.out.println(asset);
}
Split function will easily split your string like this

How to Replace dot (.) in a string in Java

I have a String called persons.name
I want to replace the DOT . with /*/ i.e my output will be persons/*/name
I tried this code:
String a="\\*\\";
str=xpath.replaceAll("\\.", a);
I am getting StringIndexOutOfBoundsException.
How do I replace the dot?
You need two backslashes before the dot, one to escape the slash so it gets through, and the other to escape the dot so it becomes literal. Forward slashes and asterisk are treated literal.
str=xpath.replaceAll("\\.", "/*/"); //replaces a literal . with /*/
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
If you want to replace a simple string and you don't need the abilities of regular expressions, you can just use replace, not replaceAll.
replace replaces each matching substring but does not interpret its argument as a regular expression.
str = xpath.replace(".", "/*/");
Use Apache Commons Lang:
String a= "\\*\\";
str = StringUtils.replace(xpath, ".", a);
or with standalone JDK:
String a = "\\*\\"; // or: String a = "/*/";
String replacement = Matcher.quoteReplacement(a);
String searchString = Pattern.quote(".");
String str = xpath.replaceAll(searchString, replacement);
return sentence.replaceAll("\s",".");

How to provide regular expression for matching $$

I am having String str = "$$\\frac{6}{8}$$"; I want to match for strings using starting with '$$' and ending with '$$'
How to write the regular expression for this?
Try using the regex:
^\$\$.*\$\$$
which in Java will be:
^\\$\\$.*\\$\\$$
A $ is a regex metacharacter used as end anchor. To mean a literal $ you need to escape it with a backslash \.
In Java \ is the escape character in a String and also in the regular expression. So to make a \ reach the regex engine you need to have \\ in the String.
See it
Use this regex string:
"^$$.*$$$"
The ^ anchors the expression to the start of the string being matched, and the last $ anchors it to the end. All other $ characters are taken literally.
You may want something like this:
final String str = "$$\\frac{6}{8}$$";
final String latex = "A display math formula " + str + " and once again " + str + " and another one " + "$$42.$$";
final Pattern pattern = Pattern.compile("\\$\\$([^$]|\\$[^$])+\\$\\$");
final Matcher m = pattern.matcher(latex);
while (m.find()) {
System.out.println(m.group());
}

Categories