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
Related
I have string such as
String url = "www.test.com/blabla/?p1=v1?p2=v2?p3=v3"
I would like to replace the "substrings" "v1","v2" and "v3" with other values. How can I achieve this?
Does something like this work for your case?
String url = "www.test.com/blabla/?p1=v1?p2=v2?p3=v3";
String result = String.format(url.replaceAll("v[0-9]", "%s"), "arg1", "arg2", "arg3");
System.out.println(result); //www.test.com/blabla/?p1=arg1?p2=arg2?p3=arg3
Edit:
Just a brief explanation of what this does, it replaces all the v1,v2,v3,v4,v5,v6,v7,v8,v9,v0 in the original url for %s and then uses this in the format method so you can attribute what you want it to be.
I need to compose an URL string in javaME (which doesn't allow me to work with many libraries which would do this in a no time) like this
url = "http://helloworld.com/index.php?data={\"Word\":"+wordX+",\"RS\":20/04/13-2008:31:44,\"SER\":"+net2+"}";
This is producion a string like this:
http://helloworld.com/index.php?data={"Word":358741051173885,"RS":20/04/13-2008:31:44,"SER":53543d303b44523d4e616fd}
The thing is I need the values to also be inside commas. I have tried everything but I'm not being able to understand how to do it.
Can someone explain this?
Thanks
Like this?
String url = String.format("http://helloworld.com/index.php?data={\"Word\":\"%s\",\"RS\":20/04/13-2008:31:44,\"SER\":\"%s\"}", wordX, net2);
Create a MessageFormat:
final MessageFormat urlFormat = new MessageFormat("http://helloworld.com/index.php?data={\"Word\":\"{0}\",\"RS\":20/04/13-2008:31:44,\"SER\":\"{1}\"}");
Then simply apply your values to the format:
final String url = urlFormat.format(new Object[]{wordX, net2});
Or, create a format String:
final String urlFormat = "http://helloworld.com/index.php?data={\"Word\":\"%s\",\"RS\":20/04/13-2008:31:44,\"SER\":\"%s\"}";
And use String.format:
final String url = String.format(urlFormat, wordX, net2);
Or, just change you String to:
url = "http://helloworld.com/index.php?data={\"Word\":\""+wordX+"\",\"RS\":20/04/13-2008:31:44,\"SER\":\""+net2+"\"}";
String val1 = "";
String val2 = "";
String str = "http://helloworld.com/index.php?data={\"Word\":\""+val1+"\",\"RS\":\"20/04/13-2008:31:44\",\"SER\":\""+val2+"\"}";
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?
In java, I want to rename a String so it always ends with ".mp4"
Suppose we have an encoded link, looking as follows:
String link = www.somehost.com/linkthatIneed.mp4?e=13974etc...
So, how do I rename the link String so it always ends with ".mp4"?
link = www.somehost.com/linkthatIneed.mp4 <--- that's what I need the final String to be.
Just get the string until the .mp4 part using the following regex:
^(.*\.mp4)
and the first captured group is what you want.
Demo: http://regex101.com/r/zQ6tO5
Another way to do this would be to split the string with ".mp4" as a split char and then add it again :)
Something like :
String splitChar = ".mp4";
String link = "www.somehost.com/linkthatIneed.mp4?e=13974etcrezkhjk"
String finalStr = link.split(splitChar)[0] + splitChar;
easy to do ^^
PS: I prefer to pass by regex but it ask for more knowledge about regex ^^
Well you can also do this:
Match the string with the below regex
\?.*
and replace it with empty string.
Demo: http://regex101.com/r/iV1cZ8
Try below code,
private String trimStringAfterOccurance(String link, String occuranceString) {
Integer occuranceIndex = link.indexOf(occuranceString);
String trimmedString = (String) link.subSequence(0, occuranceIndex + occuranceString.length() );
System.out.println(trimmedString);
return trimmedString;
}
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;