regex to replace to "" does not work - Java - java

I'm trying to replace all in a string with "" but the below does not seem to work.
str.replace("&nbsp","");
My string:
<img alt="" src="abc430.jpg" width="650" height="430" /> u seen hey hey hey
trying to get this output:
<img alt="" src="abc430.jpg" width="650" height="430" /> u seen hey hey hey
Now the replace code does replace & nbsp; with "" but still my output on the page is
u seen
link here
hey hey
It's not in one line

It works. You didn't assign the replaced string into str
str = str.replace(" ","");
Try to run this code from your end:
String str = "alex alex";
str = str.replace(" ","");
System.out.println(str);
Outputs:
alex alex

Related

ReplaceAll Regex: Update group before Replacing

I'm using regex to extract some values in groups and put those values into another section of my new string but I need to make another change to a captured group before the replaceAll executes, I have this code:
String regex = "<button data-key=\"([^\"]*)([^<]*)</button>";
while ((strLine = br.readLine()) != null) {
String newStr = strLine.replaceAll(regex, "<button data-key=\"$1$2<span>&#x$1</span></button>");
}
This works OK extracting the data-key (Group1 / $1) value into span tag when value is simple (no "-" in it), but when data-key is i.e. 1f1e8-1f1e6 the value is extracted like this: &#x1f1e8-1f1e6, so I was thinking if it's possible to do something like this:
String newStr =
strLine.replaceAll(regex,
"<button data-key=\"$1$2<span>&#x" + "$1".replaceAll("-", "&#x") + "</span></button>");
replace "-" -> "&#x" for $1 in the replaceAll function but no success so far, do I need to change to Matchers? or any help on best approach for this scenario would be appreciated, thanks.
Edit1:
CURRENT:
<button data-key="1f1e8-1f1e8-1f1e8"><span>&#x1f1e8-1f1e8-1f1e8</span></button><button data-key="1f1e8-1f1e9"><span>&#x1f1e8-1f1e9</span></button>
EXPECTED:
<button data-key="1f1e8-1f1e8-1f1e8"><span>&#x1f1e8&#x1f1e8&#x1f1e8</span></button><button data-key="1f1e8-1f1e9"><span>&#x1f1e8&#x1f1e9</span></button>
Edit2:
INPUT:
<button data-key="1f1e8-1f1e8-1f1e8"></button>
<button data-key="1f1e8-1f1e9"></button>
Edit3:
WHOLE INPUT:
<div>
<h3>GG</h3>
<div class="ep-categoryItems">
<button class="ep-item" data-key="1f1e8-1f1e8-1f1e8" title="Grinning face" style="background-image: url('${cdn}/images/emoji/f1e8-1f1e8-1f1e8.png');"></button>
<button class="ep-item" data-key="1f1e8-1f1e9" title="Grinning face" style="background-image: url('${cdn}/images/emoji/1f1e8-1f1e9.png');"></button>
</div>
<div
UPDATE: Changed to work in Java 8 and with new input.
Also fixed to add the missing ;
It can be done like this:
String input = "<button data-key=\"1f1e8-1f1e8-1f1e8\"></button><button data-key=\"1f1e8-1f1e9\"> TO BE REPLACED </button>";
String regex = "(<button data-key=\"([^\"]+)\">).*?</button>";
StringBuffer buf = new StringBuffer();
Matcher m = Pattern.compile(regex).matcher(input);
while (m.find())
m.appendReplacement(buf, m.group(1) + "<span>" + m.group(2).replaceAll("-?([0-9a-fA-F]+)", "&#x$1;") + "</span></button>");
String output = m.appendTail(buf).toString();
System.out.println(input);
System.out.println(output);
Output
<button data-key="1f1e8-1f1e8-1f1e8"></button><button data-key="1f1e8-1f1e9"> TO BE REPLACED </button>
<button data-key="1f1e8-1f1e8-1f1e8"><span>🇨🇨🇨</span></button><button data-key="1f1e8-1f1e9"><span>🇨🇩</span></button>

java find and replace %

In java 1.8.0
I am trying to replace %, but it is not matching
String str = "%28Sample text%29";
str.replaceAll("%29", "\\)");
str.replaceAll("%28", "\\(");
System.out.println("Replaced string is " + str);
I have tried all this Replace symbol "%" with word "Percent" Nothing worked for me. Thanks in Advance.
It's working.
You need re-assign to str
str = str.replaceAll("%29", "\\)");
str = str.replaceAll("%28", "\\(");
Jerry06's answer is correct.
But you could do this simply by using URLDecoder to decode those unicode value.
String s = "%28Hello World!%29";
s = URLDecoder.decode(s, "UTF-8");
System.out.println(s);
Will output :
(Hello World!)
The problem is that you misunderstood the usage of replaceall. It's for regex based replacements. What you need to use is the normal replace method like that:
String str = "%28Sample text%29";
str=str.replace("%29", "\\)"). replace("%28", "\\(");
System.out.println("Replaced string is " + str);

How to remove tag from a string

I have a string i.e
String test = "<p> My company is best in world. I love my company </p>";
I have to remove both the tags <p> and </p>.
I tried using
String replacingPtag = test.replaceAll("<p>", "");
String r1 = replacingPtag.replaceAll("</p>", "");
This code removed the <p> tag but not </p>.
How can I remove both forms of the tag?
try this regex
String res = test.replaceAll("</?p>", "");

java regex get just the filename

need some help on pattern mathcing; I need to extract just the filename from a string like:
https://www.testsite.com/files/form/anonymous/api/library/ecb198be-1f05-4b0b-b0cd-7d878488a8c4/document/050cc508-1ea6-4b5f-a22b-b3edbdf6291f/media/x.jpg
just the x.jpg part
& also from this string:
<img alt="/JAGC/Images?action=AttachFile&do=get&target=Images/x.jpg">
& if they are the same image, then replace the target with the URL string.
I can regex out the the
any help please?
It doesn't need any regexp.
Use like this:
String code = "...";
String filename = code.substring(code.lastIndexOf("/")+1, code.length());
Edit:
And in the second case, you dont need the ending of the tag, so use code.length()-2
It's as simple as this:
String filename1 = url.replaceAll(".*/([^/]+)", "$1");
String filename2 = xml.replaceAll(".*/([^\"]+)\".*", "$1");
if (filename1.equals(filename2))
xml = xml.replaceAll("(.*/)([^\"]+)(\".*)", "$1" + url + "$3");
Try this:
str.replaceAll("^.*([a-z]+\\.[a-z]+).*$","$1");
The () group the filename to $1.

How to remove " " from java string

I have a java string with " " from a text file the program accesses with a Buffered Reader object. I have tried string.replaceAll(" ","") and it doesn't seem to work.
Any ideas?
cleaned = cleaned.replace(" "," ");
cleaned = cleaned.replace("\u00a0","")
This is a two step process:
strLineApp = strLineApp.replaceAll("&"+"nbsp;", " ");
strLineApp = strLineApp.replaceAll(String.valueOf((char) 160), " ");
This worked for me. Hope it helps you too!
The same way you mentioned:
String cleaned = s.replace(" "," ");
It works for me.
There's a ready solution to unescape HTML from Apache commons:
StringEscapeUtils.unescapeHtml("")
You can also escape HTML if you want:
StringEscapeUtils.escapeHtml("")
Strings are immutable so You need to do
string = string.replaceAll(" ","")
You can use JSoup library:
String date = doc.body().getElementsByClass("Datum").html().toString().replaceAll(" ","").trim();
String.replace(char, char) takes char inputs (or CharSequence inputs)
String.replaceAll(String, String) takes String inputs and matches by regular expression.
For example:
String origStr = "bat";
String newStr = str.replace('a', 'i');
// Now:
// origStr = "bat"
// newStr = "bit"
The key point is that the return value contains the new edited String. The original String variable that invokes replace()/replaceAll() doesn't have its contents changed.
For example:
String origStr = "how are you?";
String newStr = origStr.replaceAll(" "," ");
String anotherStr = origStr.replaceAll(" ","");
// origStr = "how are you?"
// newStr = "how are you?"
// anotherStr = howareyou?"
We can have a regular expression check and replace HTML nbsp;
input.replaceAll("[\\s\\u00A0]+$", "") + "");
It removes non breaking spaces in the input string.
My solution is the following, and only this worked for me:
String string = stringWithNbsp.replaceAll("NNBSP", "");
Strings in Java are immutable. You have to do:
String newStr = cleaned.replaceAll(" ", "");
I encountered the same problem: The inner HTML of the element I needed had "&nbsp" and my assertion failed.
Since the question has not accepted any answer,yet I would suggest the following, which worked for me
String string = stringwithNbsp.replaceAll("\n", "");
P.S : Happy testing :)

Categories