other Wesites addresses did not match as url - java

Hi i am using this code to match to the editbox text(where user input web addresses)
(Patterns.WEB_URL.matcher(txt_Editbox).matches())
but when the user input this url:
http://website.info?ques==two&t=p
it did'nt accept as url, it read as a text. could anyone help me to solve this or suggest to do anything else. ??
thank you.

The URL is incorrect. It's missing a URL path separator /. Try matching with:
http://website.info/?ques=two&t=p

I had resolve this problem instead of using
(Patterns.WEB_URL.matcher(txt_Editbox).matches())
i used
String urlname = "^(https?|ftp|file)://.+$";
Matcher matcherObj = Pattern.compile(urlname).matcher(txt_Editbox);
this one can accept all kinds of web addresses as long as this address exist, and now i am able to view this site: http://website.info?ques==two&t=p to my webview.

Related

Capture a group containing URL encoded

I tried, searching this but I didn't find anything.
I found a regex pattern to extract username from facebook link:
(?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*)?
The problem with this is that it can not capture the username if it is encoded. The original username is in arabic. For example, this kind of links:
https://www.facebook.com/%D9%82%D8%B1%D9%8A
The problem is with the percentages, but how to fix it? Please help me!
Thanks to #mwp I resolved the problem by just decoding the URL before passing it to the matcher.
link = URLDecoder.decode(link.replaceAll("/$", ""), "UTF-8");
replaceAll() is to remove any trailing slashes.

Extracting Android App ID from URL

I want to extract Android application ID from URL.
Examples of URLs are:
https://play.google.com/store/apps/details?id=com.opera.mini.native
https://play.google.com/store/apps/details?id=com.opera.mini.native&referrer=xxxx
And I want to get com.opera.mini.native substrings form both URLs.
I tried to create regex to parse ID, but unsuccessfully:
^.+details\?id=(.+)&?.+
The problem is that regex returns com.opera.mini.native&referrer=xxxx for second case (for 1st URL it works fine).
How I can change regex to achieve my goal?
Thanks
It's because you made & as optional.
".+\\bdetails\\?id=([^&]+)"
Regex can be:
(?<=[?&]id=)[^&]+
ResEx Demo

how to replace brackets in url with bracket encoding?

I need a regex pattern that will find and replace brackets in urls to its urls encoding.
For example a base url like:
http://www.mysite.com/bla/blabla/abc[1].txt
will be turned to:
http://www.mysite.com/bla/blabla/abc%5B1%5D.txt
can anyone help please?
EDIT1:
i originaly use commons-httpclient to access this kind of urls.
when I use the first URL I get an "escaped absolute path no valid" exception.
I can't use URLENCODER because when I use it, I get a "host parameter is null" exception.
The following line should do the trick
String s = URLEncoder.encode("http://www.mysite.com/bla/blabla/abc[1].txt", "UTF-8");
Have you tried URLEncoder.encode?
in the java.net.URLEncoder package.
EDIT:
Ok i see... you cannot pass an entire URL to URLEncoder. URLEncoder is mostly used to encode query parameters.
try this instead:
URI uri = new URI("http", "www.mysite.com", "/bla/blabla/abc[1].txt",null);
System.out.println(uri.toASCIIString());

How to retrieve text from url using regex in Android

I have a url and I am trying to extract the text before the third slash. I am quite new to the concept in Android. I believe the Pattern class is used to achieve this. My problem is how to.
Take for instance: http://name.mywebsite.com/images.... I only require everything before images. Could anyone point me in the right direction?
You can use the uri method getHost in java.This example will help you,
URI uri = new URI("http://name.mywebsite.com/images");
String host = uri.getHost();
/* It returns name.mywebsite.com/*/

Java: How to easily check if a URL was already shortened?

If I have a general url (not restricted to twitter or google) like this:
http://t.co/y4o14bI
is there an easy way to check if this url is shortened?
In the above case, I as a human can of course see that it was shortend, but is there an automatic and elegant way?
You could do a request to the URL, look if you get redirected and if so, assume it's a shortening service. For this you'd have to read the HTTP status codes.
On the other hand, you could whitelist some URL shortening services (t.co, bit.ly, and so on) and assume all links to those domains are shortened.
Drawback of the first method is that it isn't certain, some sites use redirects internally. The drawback of the second method is that you'd have to keep adding shortening services, although only a few are used widely.
One signal may be to request the URL and see if it results in a redirect to another domain. However, without a good definition of what "shortened" means, there is no generic way.
if you know all the domains that can be used to shorten your URLs, check if it is contained :
String[] domains = {"bit.ly", "t.co"...};
for(String domain : domains){
if(url.startsWith("http://" + domain)){
return true;
}
}
return false;
You can't: You will have to work by assumption.
Assumption:
Does www exist in url.
Does the server name end with a valid domain (e.g. com, edu, etc.) or does it has co.xx where xx is a valid country or organization code.
And you can add more assumption based on other url shortening links.
You can't.
You can only check if you list a couple of shorteners and check if the url starts with it.
You can also try checking whether the url is shorter than a given length (and contains path/query string), but some shorteners (tinyurl for example) may have longer urls than normal sites (aol.com)
I would prefer the list of known shorteners.
Here's what you could do in Java, groovy and the like.
Get the url you want to test;
Open the url with HttpURLConnection
Check the response code
if it is a valid code, 200 for example, the you can retrieve the url string in long form from the connection object if it was shortened or back in its original form if it wasn't.
We all love to see some code don't we. Its crude, but hey!
String addr = "http://t.co/y4o14bI";
URL url = new URL(addr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (connection.getResponseCode() == 200) {
String longUrl = connection.url;
System.out.println(longUrl);
} else {
// You decide what you want to do here!
}
Actually, you as a human, can't. The only way you know that it's shortened is that it's a t.co domain. The y4o14bI could be an CMS identifier for all you know.
The best way would be to use a list of known shortener urls, and lookup against that.
And even then you would have problems. I use bit.ly with a personal domain, wtn.gd
So http://wtn.gd/random would also be a shortened URL.
You could maybe do a HTTP HEAD-request, and check for a 301/302 ?
If you request an URL like this, your HttpCLient should receive a HTTP Redirect instead of a HTML page. This wouldn't be an evidence but at least a hint.
Evaluate the URL and look for some clues:
the Path meets certain criteria
only has one step (i.e. not multiple slashes)
does not end with filename extensions
not longer than X characters (would need to evaluate various URL shortening services and adjust the upper bounds for the max token length)
HttpUrlConnection returns a redirect responseCode (i.e. 301, 302)
I would suggest using android.util.Patterns.WEB_URL
public static List<String> findUrls(String input) {
List<String> links = new ArrayList<>();
Matcher m = android.util.Patterns.WEB_URL.matcher(input);
while (m.find()) {
String url = m.group();
links.add(url);
}
return links;
}
Use the unshorten URL service like https://unshorten.me
They have an API as well https://unshorten.me/api
If the URL is shortened it will return the original URL.
If not you will get the same one back.

Categories