How to validate URL in java using regex? [duplicate] - java

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
I want to validate url started with http/https/www/ftp and checks for /\ slashes and checks for .com,.org etc at the end of URL using regular expression. Is there any regex patttern for URL validation?

This works:
Pattern p = Pattern.compile("(#)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?[a-zA-Z_0-9\\-]+(\\.\\w[a-zA-Z_0-9\\-]+)+(/[#&\\n\\-=?\\+\\%/\\.\\w]+)?");
Matcher m = p.matcher("your url here");

I am use the following code for that
String lRegex = "^(https?|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]";
btw a search in google and you would find the solution by yourself.

Related

Extract numbers from String using Regex [duplicate]

This question already has answers here:
Matching one string multiple times using regex in Java
(4 answers)
Closed 4 years ago.
I have a string such as below:
String str="tile tile-2 tile-position-1-4"
I wish to receive the numbers in an array,such as [2,1,4].
My own solution is to break the string using split, but i am wondering if there is a shortcut using Regx
Thanks to #nitzien with Regx it tried:
String pattern= "^tile tile-(\\d*) tile-position-(\\d*)-(\\d*)$";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
System.out.println(m.group(0));
System.out.println(m.group(1));
System.out.println(m.group(2));
But, it complains with:
java.lang.IllegalStateException: No match found
regex pattern - "^tile tile-(\d*) tile-position-(\d*)-(\d*)$"
replacement - "[\1,\2,\3]"
Replacement is string which you will need to convert to array depending on which language you are using.
Updated answer after edited question
String str="tile tile-2 tile-position-1-4";
String pattern= "^tile tile-(\\d*) tile-position-(\\d*)-(\\d*)$";
System.out.println(str.replaceAll(pattern, "$1,$2,$3"));
This will give
2,1,4

Grabbing partial data from URL in Selenium [duplicate]

This question already has answers here:
Parse a URI String into Name-Value Collection
(30 answers)
Closed 4 years ago.
In selenium, is there a way to grab partial dynamically changing data from a URL? Example, I ran driver.getCurrentUrl() in my code and retrieved the below URL. I only want to grab the numeric portion of it and concatenate it so I end up with 819499-1. However, next time I run it the values will be different. Is there a way to capture this information without capturing the entire URL?
http://custmaint/clnt_estimate_overview.jsp?estimateNbr=819499&versionNbr=1&estimateMgmt=true&clntAutoAssign=true
I think this is a more generic question, not only related to Selenium...
Basically, You could use regular expression matching like so:
String url = "http://custmaint/clnt_estimate_overview.jsp?estimateNbr=819499&versionNbr=1&estimateMgmt=true&clntAutoAssign=true";
final Pattern p = Pattern.compile("^.*\\?estimateNbr=(\\d+)&versionNbr=(\\d+).*$");
Matcher m = p.matcher(url);
if (m.find()) {
System.out.print(m.group(1) + "-" + m.group(2));
}
Another approach (more flexible) is to use a dedicated library like httpcomponents; see How to convert map to url query string?
String url = driver.getCurrentUrl();
String estimateNbr = url.substring(url.indexOf("eNbr=")+5, url.indexOf("&v"));
String versionNbr = url.substring(url.indexOf("nNbr=")+5, url.indexOf("&e"));
System.out.println(estimateNbr + "-" + versionNbr);

Decode HTML Escape Characters [duplicate]

This question already has answers here:
How to do URL decoding in Java?
(11 answers)
Closed 7 years ago.
I'm downloading a JSON from the Google Directions API. For the HTML_Instructions field, representing the actual instruction needed for navigation, here is the format:
"Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eMinor Ave\u003c/b\u003e toward \u003cb\u003eMadison St\u003c/b\u003e",
Is there a way to decode/remove the escape characters from the String that is downloaded in Java/an Android application.
Thanks for the help.
In Java use
String result = java.net.URLDecoder.decode(url, "UTF-8");
In JS use decodeURIComponent
document.write(decodeURIComponent("Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eMinor Ave\u003c/b\u003e toward \u003cb\u003eMadison St\u003c/b\u003e"))

Check String is URL in android code? [duplicate]

This question already has answers here:
How to check if URL is valid in Android
(12 answers)
Closed 8 years ago.
I allow user to input text string in my EditView. When I get Editview text, I want to validate if it is URL link or not? Can any one give Android code function? Thanks
Use WEB_URL pattern in Patterns (android.util.Patterns) Class
Patterns.WEB_URL.matcher(inputTextForURLEdittext.toLowerCase()).matches();
It will return true if URL is valid and false if URL is invalid.
For more info refer to this link
http://developer.android.com/reference/android/util/Patterns.html
You can check it by using URLUtil .
if (URLUtil.isValidUrl(urlString)) {
// URL is valid
}
Hope it will be work for you.

Java URL Encode how to encode Hello World as Hello%20World [duplicate]

This question already has answers here:
URLEncoder not able to translate space character
(19 answers)
Closed 9 years ago.
Hi Um quite new to Java and I want to encode Hello World it to Hello%20World . But when I use URLEncoder.encode it will encode the string as Hello+World and when we try to pass it via a Request to IIS it ll consider as a threat. How to encode as i mentioned ? Thank you in advance.
This only works for real URLs
URL u = new URL("Hello World");
String path = u.getPath();

Categories