I've tried different way but not working yet.
public String SuEscapeHTML(String text){
text=text.replaceAll("/&/g", "&");
// and how to deal with the double quote? text=text.replaceAll("/"/g", """);
text=text.replaceAll("/'/g", "'");
text=text.replaceAll("/</g", "<");
text=text.replaceAll("/>/g", ">");
text=text.replaceAll("/\\/g", "\");
System.out.println(text);
return text;
}
nothing change by using this function.
So How to make it working?
The syntax of regex you are using is of JavaScript. This is how you will do it in Java
String text = "&>\"<\\'"; //You need to escape " in text also
System.out.println(text.replaceAll("&", "&"));
System.out.println(text.replaceAll("\"", """)); //For quotes
System.out.println(text.replaceAll("'", "'"));
System.out.println(text.replaceAll("<", "<"));
System.out.println(text.replaceAll(">", ">"));
System.out.println(text.replaceAll("\\\\", "\"));
Ideone Demo
Related
I'm trying to create a method which can highlight text in a jlabel according user entered search text. it works fine except it case sensitive. I used a regex (?i) to ignore case. But still it case sensitive.
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
String SourceText = "this is a sample text";
String SearchText = jTextField1.getText();
if (SourceText.contains(SearchText)) {
String OutPut = "<html>" + SourceText.replaceFirst("(?i)" + SearchText, "<span style=\"background-color: #d5f4e6;\">" + SearchText + "</span>") + "</html>";
jLabel1.setText(OutPut);
} else {
jLabel1.setText(SourceText);
}
}
How can i fix this.
Update
contains is case sensitive.
How to check if a String contains another String in a case insensitive manner in Java
You have not used the matched text in the replacement, you hard-coded the same string you used in the search. Since you wrap the whole match with html tags, you need to use the $0 backreference in the replacement (it refers to the whole match that resides in Group 0).
Besides, you have not escaped ("quoted") the search term, it may cause trouble if the SearchText contains special regex metacharacters.
You can fix the code using
String OutPut = "<html>" + SourceText.replaceFirst("(?i)" + Pattern.quote(SearchText), "<span style=\"background-color: #d5f4e6;\">$0</span>") + "</html>";
I am using the wikimedia api to get content from wikipedia pages. The api returns a lot of "\n" as plain text. I want to remove them from a string
s = s.replaceAll("\\n", "");
s = s.replaceAll("\n", "");
Neither of these work, any ideas?
When your String contains a plaintext \n it is actually a \\n otherwise it would be displayed as a linebreak, which is why I found s = s.replaceAll("\\\\n","") to be working for me. An example snippet:
class Main{
public static void main(String[] args){
String s = "Hello\\nHello";
System.out.println(s);
s = s.replaceAll("\\\\n","");
System.out.println(s);
}
}
Remember that replaceAll takes a Regex: Since you want to replace 2 /s you have to escape both of them, therefore////
Hi Please to use below code format:
s= s.replace("\n", "").replace("\r", "");
Thanks
You can use the code below:
s = s.replace("\n", "");
but, the newline character can be different among the environments.
So, you can use this
s = s.replace(System.getProperty("line.separator"), "");
I am using the code in Java:
String word = "hithere";
String str = "123hithere12345hi";
output(str.replaceAll("(?!"+word+")", "x"));
However, rather than outputting: xxxhitherexxxxxxx like I want it to, it outputs: x1x2x3hxixtxhxexrxex1x2x3x4x5xhxix x, I've tried a load of different regex patterns to try to do this, but I can't seem to figure out how to do this :(
Any help would be much appreciated.
Well this technically works. Using only replace all and only one line, and it's assuming you string does not contain a deprecated ASCII character (BEL)
String string = "hithere";
String string2 = "asdfasdfasdfasdfhithereasasdf";
System.out.println(string2.replaceAll(string,"" + (char)string.length()).replaceAll("[^" + (char)string.length() + "]", "x").replaceAll("" + (char)string.length(), string));
I think this is what you're looking for, if I'm not mistaken:
String pattern = "(\\d)|(hi$)";
System.out.println("123hithere12345hi".replaceAll(pattern, "X"));
The pattern replaces any numeric digits and the word "hi".
This lookaround based code will work for you:
String word = "hithere";
String string = "123hithere12345hi";
System.out.println(string.replaceAll(
".(?=.*?\\Q" + word + "\\E)|(?<=\\Q" + word + "\\E(.){0,99}).", "x"));
//=> xxxhitherexxxxxxx
Using replaceAll() is giving me a rexex exception.
This is the code I am using:
public class test {
public static void main(String[] args) {
String text= "This is to be replaced &1 ";
text = text.replaceAll("&1", "&");
System.out.println(text);
}
}
EXCEPTION:
Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Unknown Source)
at java.util.regex.Matcher.replaceAll(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at test.main(test.java:7)
Seems to work fine for me. http://ideone.com/7qR6Z
But for something this simple, you can avoid regex and just use string.replace()
text = text.replace("&1", "&");
If you don't want regex then use String#replace method instead like this:
"This is to be replaced &1 ".replace("&1", "&")
My solution for this error while replacing with "$" sign was to replace all "$" with "\\$" like in code bellow:
myString.replaceAll("\\$", "\\\\\\$");
You can use Pattern.quote() to compile any string into a regular expression. Try:
public class test {
public static void main(String[] args) {
String text= "This is to be replaced &1 ";
text = text.replaceAll(Pattern.quote("&1"), "&");
System.out.println(text);
}
}
As it stands, your code works fine. However, if you mistyped or something and actually have
text = text.replaceAll("&1", "$");
Then you DO have to escape the replacement:
text = text.replaceAll("&1", "\\$");
Your question title shows how do i replace any string with a “$ ” in java? but your question text says String text= "This is to be replaced &1 "
If you're actually trying to replace a dollar sign, this is a special character in regular expressions, you need to escape it with a backslash. You need to escape that backslash, because blackslash is a special character in Java, so assuming dollar sign is what you intended:
String text = "This is to be replaced $1 ";
text = text.replaceAll("\\$1", "\\$");
System.out.println(text);
EDIT: Clarify some text
Greetings all.
I am using the following regex to detect urls in a string
and wrap them inside the < a > tag
public static String detectUrls(String text) {
String newText = text
.replaceAll("(?:https?|ftps?|http?)://[\\w/%.-?&=]+",
"<a href='$0'>$0</a>").replaceAll(
"(www\\.)[\\w/%.-?&=]+", "<a href='http://$0'>$0</a>");
return newText;
}
i have a problem that the following links are not detected correctly:
i am not that good with regex, so please advise.
http://code.google.com/p/shindig-dnd/
http://confluence.atlassian.com/display/GADGETDEV/Gadgets+and+JIRA+Portlets
www.liferay.com/web/raymond.auge/blog/
(www.opensocial.org/)
http://www.google.com
I'm using this:
private static final String URL_REGEX =
"http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?";
Matcher matcher = URL_PATTERN.matcher(text);
text = matcher.replaceAll("$0");
return text;
The problem you have is that you are using - within a character group ([]) without escaping it, which is being used to define the range .-? (i.e. the characters ./0123456789:;<=>?). Either escape it \\- or put it at the end of the character class so that it doesn't complete a range.
public static String detectUrls(String text) {
String newText = text
.replaceAll("(?:https?|ftps?|http?)://[\\w/%.\\-?&=]+",
"<a href='$0'>$0</a>").replaceAll(
"(www\\.)[\\w/%.\\-?&=]+", "<a href='http://$0'>$0</a>");
return newText;
}
As marcog said, you should escape the - and to match the last 2 examples you gave, you have to make the http optionnal. Also http? matches htt wich is not a correct protocol.
So the regex will be:
"(?:(?:https?|ftps?)://)?[\\w/%.?&=-]+"