I tried a couple of solutions but was unable to solve it. I looking for a solution where I can replace special characters in Path Variable in Spring Boot.
Example
xyz.com/3233+23+232+323
I am looking for a possible solution where Spring #PathVariable returns me the String "323323232323" without + sign.
I know I can do a simple String replace, but there are 100's of API's and it will be difficult to do that.
I am looking for something with minimal changes required.
One way, just read the #PathVariable value, then apply a regular expression.
Just One line, Example:
#GetMapping("/read/{str}")
public String check(#PathVariable String str){
String modifiedStr = str.replaceAll("\\+", "");
return modifiedStr;
}
Sample: http://localhost:8080/ticket-service/read/3233+23+232+323
Output: 323323232323
Related
I am defining an Apache camel route using XML configurations, and I want to call a method while passing parameters with single quotes:
<bean ref="cmdExecutor" method="execute('BatchQA.bat',
'./input/CamelCMDFile/QATestScripts/', 'Analytics,'qa.user'')"/>
The execute method looks like this:
public int execute(String bat, String dir, String arguments, Exchange exchange) {
String[] args = arguments.split(",");
result = ProcessUtils.cmdExecute(bat, dir, args);
.....
I have tried using ', ' and ' to get the required result, but neither have worked. These characters are simply being ignored in the arguments object and the rest of the string is received as it is in my java function.
After applying #Screwtape solution, argument I am getting 'qa.user' and this is not what I am aiming.
Thanks. :)
I'm not sure what Camel is doing with these single quoted strings, because it seemed just to strip the apostrophes if you quote with apostrophes such that options I expected to cause errors just seemed to work.
However, I have got it to work as you require. You need to reverse the quotation types. XML allows both single and double quotes in attributes, even though eclipse doesn't seem to colourise the single quoted attributes (but this site does).
Hence when I use
<camel:bean ref="testBean" method='test("BatchQA.bat",
"./input/CamelCMDFile/QATestScripts/", "Analytics,'qa.user'")' />
my test bean does break out the strings as you wanted:
[WARN ]: beans.testBean - Analytics
[WARN ]: beans.testBean - 'qa.user'
although I don't know if it would be possible to have a string like this with both single and double quotes. Let's hope you don't need that.
I need to match a certain Regexp pattern in Java and I think I'm very close, could anyone with more experience help? I have been testing it for at least a few hours and couldn't come to a solution yet.
This Regexp is mounted based on a URL, and must represent a "key" to this URL, since depending on the source it may change a lot, but a few stuff is always there... Already mapped Strings to match:
http://fictionalURL:8080/servlet/TPCW_new_products_servlet;jsessionid=865266C8B1231C35FEDEAA9D66400074?subject=POLITICS
http://fictionalURL.:8080/servlet/TPCW_buy_request_servlet;jsessionid=6FA80FDC52BB22518DB7D587E0876D63?RETURNING_FLAG=Y&UNAME=OGREREBABAREAT&PASSWD=ogrerebabareat&C_ID=1440046&SHOPPING_ID=171
http://localhost:8080/servlet/;jsessionid=865266C8B1231C35FEDEAA9D66400074?subject=POLITICS
my code is built so that the part that represents the URL pattern is built on runtime:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test_regexp {
public static void main (String[] args){
String testString = "http://ec2-54-158-62-71.compute-1.amazonaws.com:8080/servlet/TPCW_buy_request_servlet;jsessionid=6FA80FDC52BB22518DB7D587E0876D63?RETURNING_FLAG=Y&UNAME=OGREREBABAREAT&PASSWD=ogrerebabareat&C_ID=1440046&SHOPPING_ID=171";
int beginIndex = testString.indexOf("servlet");
int endIndex = testString.indexOf("jsessionid");
CharSequence cs = new String(testString);
String patt = "\\(?=.*:8080/.*)(?=.*jsessionid=).*";
System.out.println("Pattern: "+patt);
Pattern teste = Pattern.compile(patt);
System.out.println(teste.matcher(cs).matches());
}
}
but at the end the pattern should look something like this:
Pattern: ((?=.:8080/.)(?=.jsessionid=).)
PS: The pattern must include the URL full endpoint (with parameters), but not the sessionId and other stuff
EDIT: I forgot to mention, the regexp must also have the subject parameter, which is after the session ID, I have only realized it while writing this...
For those who want to know what's my purpose on all that, I'm making a LRU cache based on Regexp Patterns stored in a HashSet.
I would apprecite the help very much! This is the last task to finish the project!
Thanks in advance.
Double check your pattern. I'm not sure what the backslash out front is for.
"\\(?=.*:8080/.*)(?=.*jsessionid=).*"
Your pattern would also happily match jsessionid=10:8080/
I have a URL and I want to print in my graphical user interface the ID value after the hashtag.
For example, we have www.site.com/index.php#hello and I want to print hello value on a label in my GUI.
How can I do this using Java in Netbeans?
Simple solution is getRef() in URL class:
URL url = new URL("http://www.anyhost.com/index.php#hello");
jLabel.setText(url.getRef());
EDIT: According to #Henry comment:
I would recommend to use the java.net.URI as it also deals with encoding. The Javadocs say: "Note, the URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL()."
and this comment:
Why not just doing uri.getFragment()
URI uri = new URI("http://www.anyhost.com/index.php#hello");
jLabel.setText(uri.getFragment());
Use the String.split() Method.
public static String getId(string url) {
return url.split("#")[1];
}
String.split() returns an array of Strings that are delimited, or "Split," by the value you pass to it, or in this case #.
Because you want only the string after the #, you can just use the second item in the array that it returns by adding [1] to the end of it.
For more on String.split() go to Tutorials Point.
By the way, the part of the URL you are referencing is the Element ID. It is used to jump to an Element on a webpage.
I have a string formatted as below:
source1.type1.8371-(12345)->source2.type3.3281-(38270)->source4.type2.903..
It's a path, the number in () is the weight for the edge, I tried to split it using java Pattern as following:
[a-zA-Z.0-9]+-{1}({1}\\d+){1}
[a-zA-Z_]+.[a-zA-Z_]+.(\\d)+-(\\d+)
[a-zA-Z.0-9]+-{1}({1}\\d+){1}-{1}>{1}
hopefully it split the string into fields like
source1.type1.8371-(12345)
source2.type3.3281-(38270)
..
but none of them work, it always return the whole string as the field.
It looks like you just want String.split("->") (javadoc). This splits on the symbol -> and returns an array containing the parts between ->.
String str = "source1.type1.8371-(12345)->source2.type3.3281-(38270)->source4.type2.903..";
for(String s : str.split("->")){
System.out.println(s);
}
Output
source1.type1.8371-(12345)
source2.type3.3281-(38270)
source4.type2.903..
It seems to me like you want to split at the ->'s. So you could use something like str.split("->") If you were more specific about why you need this maybe we could understand why you were trying to use those complicated regexes
I have this string: "\"Blah \'Blah\' Blah\"". There is another string inside it. How do I convert that into: Blah 'Blah' Blah? (you see, unescaping the string.) This is because I get a SQL Where query:
WHERE blah="Blah \'Blah\' Blah"
When I parse this, I get the string above (still inside quotes and escaped.) How would I extract that, un-escaping the string? Or is ther some much easier way to do this? Thanks,
Isaac
DO NOT DO THIS.
Follow the proper steps for parametrization of a query on your Database/Platform, and you won't have to escape anything. You also will protect yourself from injection vulnerabilities.
Put the string in a property file, Java supports XML property files and the quote character does not need to be escaped in XML.
Use loadFromXML(InputStream in) method of the Properties class.
You can then use the MessageFormat class to interpolate values into the String if needed.
This should be about right. This assumes that if it starts with a quote, it ends with a quote.
if (val.startsWith("\"") || val.startsWith("\'"))
val = val.substring(1, val.length-2);
You may wish to add val = val.trim(); as well.
"\"Blah \'Blah\' Blah\"".replaceAll("\"", "")