Dynamically replace part in URL using Regex - java

I tried searching for something similar, and couldn't find anything. I'm having difficulty trying to replace a few characters after a specific part in a URL.
Here is the URL: https://scontent-b.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/s130x130/10390064_10152552351881633_355852593677844144_n.jpg?oh=479fa99a88adea07f6660e1c23724e42&oe=5519DE4B
I want to remove the /v/ part, leave the t1.0-9, and also remove the /s130x130/.I cannot just replace s130x130, because those may be different variables. How do I go about doing that?
I have a previous URL where I am using this code:
if (pictureUri.indexOf("&url=") != -1)
{
String replacement = "";
String url = pictureUri.replaceAll("&", "/");
String result = url.replaceAll("().*?(/url=)",
"$1" + replacement + "$2");
String pictureUrl = null;
if (result.startsWith("/url="))
{
pictureUrl = result.replace("/url=", "");
}
}
Can I do something similar with the above URL?

With the regex
/v/|/s\d+x\d+/
replaced with
/
It turns the string from
https://scontent-b.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/s130x130/10390064_10152552351881633_355852593677844144_n.jpg?oh=479fa99a88adea07f6660e1c23724e42&oe=5519DE4B
to
https://scontent-b.xx.fbcdn.net/hphotos-xpf1/t1.0-9/10390064_10152552351881633_355852593677844144_n.jpg?oh=479fa99a88adea07f6660e1c23724e42&oe=5519DE4B
as seen here. Is this what you're trying to do?

Related

whitespace will not get removed

Assume there is a url="www.example.com/". Using the following code I want to remove the trailing slash but it leaves a blank at the end of string (which by the way I do not know why) and using the rest of the code, I am trying to remove the white space but it will not work.
String url="http://www.example.com/";
int slash=url.lastIndexOf("/");
StringBuilder myURL = new StringBuilder(url);
if(url.endsWith("/")){
myURL.setCharAt(slash, Character.MIN_VALUE );
url=myURL.toString();
}
url=url.replaceAll("\\s+","");
System.out.println(url);
Try to trim it: url = url.trim();
Because \s+ does not match Character.MIN_VALUE. Use ' ' instead.
String url="www.example.com/";
int slash=url.lastIndexOf("/");
StringBuilder myURL = new StringBuilder(url);
if(url.endsWith("/")){
myURL.setCharAt(slash, ' ');
url=myURL.toString();
}
url=url.replaceAll("\\s+","");
System.out.println(url);
But why don't you just remove the / ?
String url="www.example.com/";
int slash=url.lastIndexOf("/");
StringBuilder myURL = new StringBuilder(url);
myURL.deleteCharAt(slash);
System.out.println(myURL);
String url="www.example.com/";
if(url.endsWith("/")){
url = url.substring(0, url.length()-1);
}
System.out.println(url);
Instead of setCharAt() you should use deleteCharAt().
But the simplest way to do the job is
String url="www.example.com/";
url = url.substring(0, url.lastIndexOf("/"));
The issue appears to be the use of the setCharAt method.
This method replaces a char with another char. So even though you have replaced it with the Character.MIN_VALUE which at first glance may appear to represent the literal Null it is actually still a unicode character ('\0000' aka the null character).
The simplest fix would be to replace...
myURL.setCharAt(slash, Character.MIN_VALUE );
with...
myURL.deleteCharAt(slash);
Further info regarding the null character...
Understanding the difference between null and '\u000' in Java
what's the default value of char?
This is my first answer so apologies if I've not kept to conventions.
I think the empty space is caused by Character.MIN_VALUE being interpreted as a space.
Try this. Its a little cleaner than your current replace code and will leave no space.
if(url.endsWith("/")){
url = url.trim().substring(0, url.length-1);
}
Replace you code inside your if-block with the below one
url = url.substring(0, url.length()-1).trim();
Then I hope you will no longer need that StringBuilder object also.
So your final code will look like
String url="www.example.com";
url = (url.endsWith("/")) ? url.substring(0, url.length()-1) : url;
System.out.print(url);
Why are you complicating things, if this can be achieved in single line
String url="www.example.com/";
url=url.replace("/","");
System.out.println(url);

Substring of a link String

I would like to get only the actual link of the following link string:
String link = Link to Facebook
The result should be only www.facebook.com/wwwausedu
I tried the following but it is not working:
TEMP = link.substring(link.indexOf("http://")+1, tmp.lastIndexOf("\""));
You don't need the last index of ", but the first one after your http://:
TEMP = link.substring(link.indexOf("http://")+7, link.indexOf("\"", link.indexOf("http://")));
The String.indexOf(String str, int fromIndex) function gets the first occurence of str after the specified index. Also, as pointed out by #mellamokb the Wise, you need to add 7 to the index, not 1, since you want to exclude http:// from the result.
Why not use tool specially designed for parsing HTML like jsoup.
String link = "<a href=\"http://www.facebook.com/wwwausedu\" "
+ "target=\"_blank\" class=\"btnFacebook\">Link to Facebook</a>";
Document doc = Jsoup.parse(link);
String address = new URL(doc.select("a").attr("href")).toString();
This will return: http://www.facebook.com/wwwausedu but we just want part without protocol so lets use URL now
URL url=new URL(address);
System.out.println(url.getHost()+url.getPath());
Output:
www.facebook.com/wwwausedu
Try using Regex
Pattern p = Pattern.compile("href=\"(.*?)\"");
Matcher m = p.matcher(link);
String url = null;
if (m.find()) {
url = m.group(1); // this will give you the URL
}
Edit:
To remove the http too use the Regex "href=\"http://(.*?)\""

How to extract a url from a string in Java?

I have a string containing a short-code which looks like the one below:
some text...
[video url="http://www.example.com/path/to/my/video.ext"]
...some more text...
I want to be able to first check if the string contains that short-code and second extract the URL from it in Java (specifically Android).
use this regex for checking and grabbing url:
\[\w+\s+url="(?<urllink>)[^"]*"\s*]
and get gorup named urllink
try as:
String str = "[video url=\"http://www.example.com/path/to/my/video.ext\"]";
if (str.contains("url=\""))
{
int indexoff = str.indexOf("url=\"");
int indexofff = str.indexOf("\"]");
String strurl = str.substring(indexoff, indexofff - indexoff);
strurl = strurl.Replace("url=\"", ""); //get url string here
}
Android provides several function for this purpose. SOme of this are:
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html
http://developer.android.com/reference/org/apache/http/client/utils/URLEncodedUtils.html
String url = "whatever"
Boolean myBool = url.contains("ate");
String contains. Not sure what extract url means, but the string class has lots of useful functions.
A powerful and maintainable manner it to Java URL.class, then, you can mix with Regex

Java: getting parameters from a URI who contains a file

let's say I have a file located in:
http://example.com/123.app
now I get the file name using the following (u is an entire url string):
String fileName = u.substring( u.lastIndexOf('/')+1, u.length() );
but I want to put on the same file name also parameters, so it'll look like, this:
http://example.com/123.app?id=87983
And I want to have a String fileName which will contain '123.app', and also String id which will contain '87983' and possibly more parameters.
How would I go about achieving this?
Firstly, take a look at this post, which uses the URL class to make working with the different parts of the URL string a lot easier.
Could you share a link to an URL parsing implementation?
Secondly, you would need to take the Query part of the URL and the Path part of the URL and substring the returned values to get the information that you desire. It should be pretty straight forward.
Use the API of URI! That's what it's for. Forget all this substring/regex/spit stuff.
you need to use the split method on string. So for example on your fileName string
String[] mystrings = fileName.split("?");
then mystrings[0] is your filename and mystrings[1] is your parameter
A simple way is : just repeat substring :
int qidx = filename.indexOf("?");
String realFilename = filename.substring(0, qidx);
String parameters = filename.substring(qidx+1);
and so on for parsing parameters.
If you are writing a servlet try :
String fileName = request.getServletPath();
and for the parameters somthing like
String id = request.getParameter("id");
Try this regex:
String s = "http://example.com/123.app?id=87983";
String[] split = s.split(".*/|\\?id=");
String filename = split[1];
String id = (split.length == 3) ? split[2] : null;

Problem in replacing special characters in a String in java

I got a String as response from server which is like the below:
hsb:\\\10.217.111.33\javap\Coventry\
Now I want to parse this string in such a way that I need to replace all \ with /.
Also I need to remove the first part of the String which is hsb:\\\
So, my resultant string should be of like this :
10.217.111.33/javap/coventry/
Can anyone help me by providing sample java code for this problem.
Here you have a "dirty" startup "solution":
String s = "hsb:\\\\\\10.217.111.33\\javap\\Coventry\\";
String w = s.replace('\\', '/');
String x = w.replace("hsb:///", "");
String result = yourString.substring(7);
result = result.replaceAll("\\\\", "/");

Categories

Resources