String replace all special characters in the end - java

Am very poor in regex, so please bear with me.
I have strings LQiW0/QIDAQAB/ and LQiW0/QIDAQAdfB/.
I'm trying to remove the last forward slash.
Tried str= str.replaceAll("\\/","");
I tried replace all but it replaces all forward slashes.. and the thing is, I want to replace if it is at last position

Try following code:
str = str.replaceAll("\\/$", "");
$ means end of line (in this case, end of string).

Do you really need regex? A simple substring will do the job:
str = str.substring(0, str.lastIndexOf("/"));
But, if you want to replace the forward slash only if it is the end of the string, then replaceAll would be good there.
But you can also use this (This might not be more readable compared to replaceAll):
str = str.endsWith("/") ? str.substring(0, str.length() - 1) : str;

It's better not to use regex replacements for these trivial operations. People tend to use regular expressions all the time even when they are not needed. Also, regular expressions can be very straight forward but get ugly pretty fast when you need to cover some side cases. See https://softwareengineering.stackexchange.com/questions/113237/when-you-should-not-use-regular-expressions
In your case there's a good tool for the job.
You can use org.apache.commons.lang.StringUtils
StringUtils.stripEnd("LQiW0/QIDAQAdfB/", "/") = "LQiW0/QIDAQAdfB"
StringUtils.stripEnd("LQiW0/QIDAQAdfB///", "/") = "LQiW0/QIDAQAdfB"
StringUtils.stripStart("///LQiW0/QIDAQAdfB/", "/") = "LQiW0/QIDAQAdfB/"
StringUtils.stripStart("///LQiW0/QIDAQAdfB///", "/") = "LQiW0/QIDAQAdfB///"

str = str.replaceAll(#"\/(?=\n)", "");
This should match a forward slash that is followed by a new line.

If you are going to have strings like LQiW0/QIDAQAB/Sdf4s and you want to remove the last / to obtain LQiW0/QIDAQABSdf4s, then this will work.
str = str.substring(0,str.lastIndexOf('/'))+str.substring(str.lastIndexOf('/')+1);
It will also work for cases with the last character /.

Related

How to replace special character }} in a string with "" using regexp in java

I am trying to replace special character }} in a string with "" using regexp in Java, I tired the below two methods and it doesn't work. Please let me know what is wrong with these statements.
Note the string would also contain } which I would like to retain. Goal is to replace only }}.
Method 1:
String buffer = obj.toJSONString() + ",";
String result = buffer.replaceAll(Pattern.quote("(?<![\\w\\d])}}(?![\\w\\d])"), "");
Method 2:
Pattern.compile("(?<![\\w\\d])}}(?![\\w\\d])").matcher(buffer).replaceAll("");
The quote in the following:
String result = buffer.replaceAll(Pattern.quote("(?<![\\w\\d])}}(?![\\w\\d])"), "");
says to treat the regex as a literal string. That's wrong.
If you simply want to remove all }} irrespective of context:
String result = buffer.replaceAll(Pattern.quote("}}"), "");
If you do need to respect the context, don't Pattern.quote(...) the regex!
The other problem is in the way that you attempt to specify the character classes. Since \d is a subset of \w, it is unnecessary to combine them. Just do this instead:
String result = buffer.replaceAll("(?<!\\w)\\}\\}(?!\\w)"), "");
I'm not sure if it is strictly necessary to quote the } characters, but it is harmless if it is not necessary.
Dont' use Pattern.quote, use a literal regex pattern, and escape the brackets:
Stringbuffer = obj.toJSONString() + ",";
String result = buffer.replaceAll("(?<![\\w\\d])\\}\\}(?![\\w\\d])", "");
Using Pattern.quote tells the regex engine to treat the string as literal. This does mean the brackets would not have to be escaped, but it would also render your lookarounds as literal text, probably not what you have in mind.
The method 2 still needs to escape special characters }
Pattern.compile("(?<![\\w\\d])\\}\\}(?![\\w\\d])").matcher(buffer).replaceAll("");
Can you please try same with Apache StringUtils. It’s faster and should work in your case. Kindly find following links for reference.
apache-stringutils-vs-java-implementation-of-replace
Apache StringUtils 3.6

Java regex for inserting text between braces

I have a String
a = "stringWithBraces()"
I want to create the following string
"stringWithBraces(text)"
How do I achieve this using regex?
I tried this :
a.replaceAll("\\(.+?\\)", "text");
But get this :
stringWithBraces()
You can use lookaheads and do something like this:
(?<=\().*?(?=\))
Live Demo
Thus doing this:
String a = "stringWithBraces()";
a = a.replaceAll("(?<=\\().*?(?=\\))", Matcher.quoteReplacement("text"));
System.out.println(a);
Outputs:
stringWithBraces(text)
Note that in relation to replaceAll() then the replacement string has some special character. So you should most likely use Matcher.quoteReplacement() in order to escape those and be safe.
You can use this :
a = a.replaceAll("\\((.*?)\\)", "(text)");
You have to replace every thing between parenthesis with (text)
+ requires at least one char, the ? added here means the shortest match, so "...(.)...(.)..." would not continue to find ".)...(.".
a.replaceAll("\\(.*?\\)", "(text)");
You might have intended replaceFirst; though I think not.
You might also let the dot . match new line chars, for mult-line matches,
using the DOT_ALL option (?s):
a.replaceAll("(?s)\\(.*?\\)", "(text)");

Java replaceAll with regex only replacing first instance

I am trying to update all url's in a CSS string and my regex only seems to get the first one. I want to get anything like:
url("file")
url('file');
url(file);
I also want to exclude things where the url is data:
url("data: ...");
url('data: ...');
url(data: ...);
I wrote some code to do this, but it only replaces the first one:
String str = ".ff0{font-family:sans-serif;visibility:hidden;}#font-face{font-family:ff1;src:url(f1.woff)format(\"woff\");}.ff1{font-family:ff1;line-height:1.330566;font-style:normal;font-weight:normal;visibility:visible;}#font-face{font-family:ff2;src:url(f2.woff)format(\"woff\");}.ff2{font-family:ff2;line-height:1.313477;font-style:normal;font-weight:normal;visibility:visible;}#font-face{font-family:ff3;src:url(f3.woff)format(\"woff\");}.ff3{font-family:ff3;line-height:1.386719;font-style:normal;font-weight:normal;visibility:visible;}#font-face{font-family:ff4;src:url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI1IiBoZWlnaHQ9IjUiPgo8cmVjdCB3aWR0aD0iNSIgaGVpZ2h0PSI1IiBmaWxsPSIjOWU5ZTllIj48L3JlY3Q+CjxwYXRoIGQ9Ik0wIDVMNSAwWk02IDRMNCA2Wk0tMSAxTDEgLTFaIiBzdHJva2U9IiM4ODgiIHN0cm9rZS13aWR0aD0iMSI+PC9wYXRoPgo8L3N2Zz4=)format(\"woff\");";
str = str.replaceAll("url\\((['\"]?)(?!data)(.*)\\1\\)","url(someURL/$2)");
out.println(str);
Any ideas on how to fix? I imagine it has something to do with the regex.
You probably want to use non-greedy quantifier (*? instead of *).
To exclude the data entries properly, also use possessive quantifier for capturing the qoutes: ?+ instead of ?.
So your regex should look as follows:
url\((['"]?+)(?!data)(.*?)\1\)
Note that you should probably escape some characters with extra slash as you did in your example.
Your .* is greedy. It's capturing to the end of the string. Use .*?, instead, which will force the engine to capture as few characters as possible.
str = str.replaceAll("url\\((['\"]?)(?!data)(.*?)\\1\\)","url(someURL/$2)");
Something like this should work:
~\((?!.*data).+\)~

Java regex for wrapping everything but some given character

I have a String such as
somet3x70rnumb3r5.3.1*#:ch4r5*
I need to wrap everything that isn't *, star character, with a Pattern Quote \Q...\E and replace the * with .*. It should give this:
\Qsomet3x70rnumb3r5.3.1\E.*\Q#:ch4r5\E.*
I can do this with string traversal, splitting on * (or any character I specify), and building a string step by step, but I'd like to do use regexes and Pattern class utilities if possible.
Another example with specified character ? which would be replaced by .:
123?4?
should give
\Q123\E.\Q4\E.
I was thinking of using groups, but I need groups around every zone because each has to be either wrapped or replaced by another character.
My goal is to create a Pattern String from a given String but only consider the areas matching the specified character and ignoring the rest (even if it contains regex patterns).
Something like this?
String s = "abc*efg?123";
s = s.replaceAll("([^\\*\\?]+)", "\\\\Q$1\\\\E");
s = s.replaceAll("\\*", ".*");
s = s.replaceAll("\\?", ".");
Results in \Qabc\E.*\Qefg\E.\Q123\E
It'll be simpler if you don't worry about building a one-liner. A one-liner is probably possible, but it will be a pain. Instead, I suggest you do something like this:
str = str.replaceAll("(?<!^)\\*(?!$)", "\\E.*\\Q")
.replaceAll("(?<!^)\\?(?!$)", "\\E.\\Q");
str = "\\Q" + str + "\\E";
Simpler to write, and much easier to understand.

Replace a word that is not on a string

I'm trying to replace a word in a file whenever it appears except when it is contained in a string:
So I should replace this in
The test in this line consists in ...
But should not match in :
The test "in this line" consist in ...
This is what I'm trying:
line.replaceAll( "\\s+this\\s+", " that ")
But it fails with this scenario so I tried using:
line.replaceAll( "[^\"]\\s+this\\s+", " that ")
But doesn't work either.
Any help would be appreciated
This seems to work (in so far as I understand your requirements from the examples provided):
(?!.*\s+this\s+.*\")\s+this\s+
http://rubular.com/r/jZvR4XEbRf
You may need to adjust the escaping for java.
This is a bit better actually:
(?!\".*\s+this\s+)(?!\s+this\s+.*\")\s+this\s+
The only reliable way to do this is to search for EITHER a complete, quoted sequence OR the search term. You do this with one regex, and after each match you determine which one you matched. If it's the search term, you replace it; otherwise you leave it alone.
That means you can't use replaceAll(). Instead you have to use the appendReplacement() and appendTail() methods like replaceAll() itself does. Here's an example:
String s = "Replace this example. Don't replace \"this example.\" Replace this example.";
System.out.println(s);
Pattern p = Pattern.compile("\"[^\"]*\"|(\\bexample\\b)");
Matcher m = p.matcher(s);
StringBuffer sb = new StringBuffer();
while (m.find())
{
if (m.start(1) != -1)
{
m.appendReplacement(sb, "REPLACE");
}
}
m.appendTail(sb);
System.out.println(sb.toString());
output:
Replace this example. Don't replace "this example." Replace this example.
Replace this REPLACE. Don't replace "this example." Replace this REPLACE.
See demo online
I'm assuming every quotation mark is significant and they can't be escaped--in other words, that you're working with prose, not source code. Escaped quotes can be dealt with, but it greatly complicates the regex.
If you really must use replaceAll(), there is a trick where you use a lookahead to assert that the match is followed by an even number of quotes. But it's really ugly, and for large texts you might find it prohibitively expensive, performance-wise.

Categories