I am using this regex to remove all escape symbols from my string svg .replaceAll("\\{", "{") I tested it in a simple main method like and it works fine
System.out.println("<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" class=\"highcharts-root\" style=\"font-family:"lucida grande", "lucida sans unicode", arial, helvetica, sans-serif;font-size:12px;\" xmlns=\"http://www.w3.org/2000/svg\" width=\"600\" height=\"350\"><desc>Created"
+ " with Highcharts 5.0.7</desc><defs><clipPath id=\"highcharts-lqrco8y-45\"><rect x=\"0\" y=\"0\" width=\"580\" height=".replaceAll("\\{", "{"));
When i tried to use this in my code there is no exception but the replace all function seems no to work.
#RequestMapping(value = URL, method = RequestMethod.POST)
public String svg(#RequestBody String svg) throws TranscoderException, IOException {
String result = svg;
String passStr = (String) result.subSequence(5, result.length() - 2);
passStr = passStr.replaceAll("\\{", "{");
InputStream is = new ByteArrayInputStream(Charset.forName("UTF-8").encode(passStr).array());
service.converter(is);
return result;
}
Try it this way:
public static void main(String[] args) {
String test = "abc\\{}def";
System.out.println("before: " + test);
System.out.println("after: " + test.replaceAll("\\\\[{]", "{"));
}
Output
before: abc\{}def
after: abc{}def
Your first example doesn't have any "{" characters, so I'm not surprised it works (??).
But anyway, your regex is wrong. Backslash is in an escape character in both Java Strings and in regular expressions. So \\ in a string just means \. That means your regex is actually just \{ , which just means {. So all you are really doing is replacing { with {.
If you want to make a regex that replaces \{ with {, you need to double each of your backslash characters in the regex: \\\\{.
Related
I want to change all letters from a string to "-" char except space using Java.
I tried:
String out = secretWord.replaceAll("^ " , "-");
and
String out = secretWord.replaceAll("\\s" , "-");
They didn't work.
I tried:
String newWord = secretWord.replaceAll("[A-Z]" , "-");
It worked but i didn't change Turkish characters I use in that string.
Original Code:
public class ChangeToLine {
public static void main(String[] args) {
String originalWord = "ABİDİKUŞ GUBİDİKUŞ";
String secretWord = originalWord;
}
}
You can use the \\S regex:
String s = "Sonra görüşürüz";
String replaced = s.replaceAll("\\S", "-");
System.out.println(replaced); // outputs ----- ---------
Use a character class
String out = secretWord.replaceAll("[^ ]" , "-");
or a capital S, instead of a lower s to replace all non space chars
String out2 = secretWord.replaceAll("\\S" , "-");
NOT needs to be expressed in square brackets in java.util.regex.Pattern:
String out = secretWord.replaceAll("[^\\s]", "-")
I was trying to replace/remove any string between - <branch prefix> /
Example:
String name = Application-2.0.2-bug/TEST-1.0.0.zip
expected output :
Application-2.0.2-TEST-1.0.0.zip
I tried the below regex, but it's not working accurate.
String FILENAME = 2.2.1-Application-2.0.2-bug/TEST-1.0.0.zip
println(FILENAME.replaceAll(".+/", ""))
There can be many ways e.g. you can replace \w+\/ with a "". Note that \w+ means one or more word characters.
Demo:
public class Main {
public static void main(String[] args) {
String FILENAME = "Application-2.0.2-bug/TEST-1.0.0.zip";
FILENAME = FILENAME.replaceAll("\\w+\\/", "");
System.out.println(FILENAME);
}
}
Output:
Application-2.0.2-TEST-1.0.0.zip
ONLINE DEMO
I have the following function to replace smileys in a String:
public String replaceSmileys(String text) {
for (Entry < String, String > smiley: smileys.entrySet())
text = text.replaceAll(smiley.getKey(), smiley.getValue());
return text;
}
static HashMap < String, String > smileys = new HashMap < String, String > ();
smileys.put("&:\\)", "<img src='http://url.com/assets/1.png'/>");
smileys.put("&:\\D", "<img src='http://url.com/assets/2.png'/>");
smileys.put("&;\\)", "<img src='http://url.com/assets/3.png'/>");
String sml = replaceSmileys(msg);
Im getting this error:
java.util.regex.PatternSyntaxException: Unknown character property name {} near index 4
&:\P
Any ideas what im doing wrong?
Only your parentheses need to be escaped, not your literal characters. So:
smileys.put("&:\\)", "<img src='http://url.com/assets/1.png'/>");
smileys.put("&:D", "<img src='http://url.com/assets/2.png'/>");
smileys.put("&;\\)", "<img src='http://url.com/assets/3.png'/>");
Note change on second line.
Basically, if you don't escape the close-parentheses, the parser gets confused because it thinks it has missed an open-parenthesis. So you must escape the parentheses. On the other hand, plain-old letters (D in your example) don't require escaping, since they don't form a part of a regex construct.
The code segment should work perfectly except that if the second pattern intends to match a smiley and not an & followed by a : and then a non-digit character, then it should be.
smileys.put("&:D", "<img src='http://url.com/assets/2.png'/>");
Its working fine for me
public class Test {
public static void main(String[] args) {
String sml = replaceSmileys("&:)");
System.out.println(sml);
}
static String replaceSmileys(String text) {
HashMap < String, String > smileys = new HashMap < String, String > ();
smileys.put("&:\\)", "<img src='http://url.com/assets/1.png'/>");
smileys.put("&:D", "<img src='http://url.com/assets/2.png'/>");
smileys.put("&;\\)", "<img src='http://url.com/assets/3.png'/>");
for (Entry < String, String > smiley: smileys.entrySet())
text = text.replaceAll(smiley.getKey(), smiley.getValue());
return text;
}
}
Output -
<img src='http://url.com/assets/1.png'/>
Is there a simple solution to parse a String by using regex in Java?
I have to adapt a HTML page. Therefore I have to parse several strings, e.g.:
href="/browse/PJBUGS-911"
=>
href="PJBUGS-911.html"
The pattern of the strings is only different corresponding to the ID (e.g. 911). My first idea looks like this:
String input = "";
String output = input.replaceAll("href=\"/browse/PJBUGS\\-[0-9]*\"", "href=\"PJBUGS-???.html\"");
I want to replace everything except the ID. How can I do this?
Would be nice if someone can help me :)
You can capture substrings that were matched by your pattern, using parentheses. And then you can use the captured things in the replacement with $n where n is the number of the set of parentheses (counting opening parentheses from left to right). For your example:
String output = input.replaceAll("href=\"/browse/PJBUGS-([0-9]*)\"", "href=\"PJBUGS-$1.html\"");
Or if you want:
String output = input.replaceAll("href=\"/browse/(PJBUGS-[0-9]*)\"", "href=\"$1.html\"");
This does not use regexp. But maybe it still solves your problem.
output = "href=\"" + input.substring(input.lastIndexOf("/")) + ".html\"";
This is how I would do it:
public static void main(String[] args)
{
String text = "href=\"/browse/PJBUGS-911\" blahblah href=\"/browse/PJBUGS-111\" " +
"blahblah href=\"/browse/PJBUGS-34234\"";
Pattern ptrn = Pattern.compile("href=\"/browse/(PJBUGS-[0-9]+?)\"");
Matcher mtchr = ptrn.matcher(text);
while(mtchr.find())
{
String match = mtchr.group(0);
String insMatch = mtchr.group(1);
String repl = match.replaceFirst(match, "href=\"" + insMatch + ".html\"");
System.out.println("orig = <" + match + "> repl = <" + repl + ">");
}
}
This just shows the regex and replacements, not the final formatted text, which you can get by using Matcher.replaceAll:
String allRepl = mtchr.replaceAll("href=\"$1.html\"");
If just interested in replacing all, you don't need the loop -- I used it just for debugging/showing how regex does business.
i have a space before a new line in a string and cant remove it (in java).
I have tried the following but nothing works:
strToFix = strToFix.trim();
strToFix = strToFix.replace(" \n", "");
strToFix = strToFix.replaceAll("\\s\\n", "");
myString.replaceAll("[ \t]+(\r\n?|\n)", "$1");
replaceAll takes a regular expression as an argument. The [ \t] matches one or more spaces or tabs. The (\r\n?|\n) matches a newline and puts the result in $1.
try this:
strToFix = strToFix.replaceAll(" \\n", "\n");
'\' is a special character in regex, you need to escape it use '\'.
I believe with this one you should try this instead:
strToFix = strToFix.replace(" \\n", "\n");
Edit:
I forgot the escape in my original answer. James.Xu in his answer reminded me.
Are you sure?
String s1 = "hi ";
System.out.println("|" + s1.trim() + "|");
String s2 = "hi \n";
System.out.println("|" + s2.trim() + "|");
prints
|hi|
|hi|
are you sure it is a space what you're trying to remove? You should print string bytes and see if the first byte's value is actually a 32 (decimal) or 20 (hexadecimal).
trim() seems to do what your asking on my system. Here's the code I used, maybe you want to try it on your system:
public class so5488527 {
public static void main(String [] args)
{
String testString1 = "abc \n";
String testString2 = "def \n";
String testString3 = "ghi \n";
String testString4 = "jkl \n";
testString3 = testString3.trim();
System.out.println(testString1);
System.out.println(testString2.trim());
System.out.println(testString3);
System.out.println(testString4.trim());
}
}