How to split URL and take only the host names [duplicate] - java

This question already has answers here:
What is the fastest way to get the domain/host name from a URL?
(8 answers)
Closed 3 years ago.
Im trying to split URLs, for example https://stackoverflow.com/questions/ and take only stackoverflow.com. How can I do this in Java without using the built in function getHost()?

If you can put your URL into a String , there is this option :
public static void main(String []args){
String str ="https://stackoverflow.com/questions/";
String[] parts = str.split("/");
String part1 = parts[0]; //https:
String part2 = parts[1]; //'nothing'
String part3 = parts[2]; //stackoverflow.com
String part4 = parts[3]; //questions
}

One thing you can do is use String#replaceAll. I know it's not what you want but off the bat it's a decent way to do it.
String uri = "https://stackoverflow.com/questions/";
if (uri.contains("https://")) {
uri = uri.replaceAll("https://", "");
}
if (uri.contains("http://")) {
uri = uri.replaceAll("http://", "");
}
int indexOfBackwardsSlash = uri.indexOf("/");
if (indexOfBackwardsSlash != -1) {
uri = uri.substring(0, indexOfBackwardsSlash);
}
Use URI#getPath.
URI uri = URI.create("https...");
String path = uri.getPath();

You could also use regular expressions:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UrlRegex {
public static void main(String []args){
String url = "https://stackoverflow.com/questions/";
Pattern pat = Pattern.compile("//([^/]*)"); //match everything between "//" and "/"
Matcher m = pat.matcher(url);
if (m.find()) {
String hostname = m.group(1);
}
}
}

Here you go :
Pattern pattern = Pattern.compile("^(?:(?:http)s?://)?(?<hostGroup>[^/:]+).*");
Matcher matcher = pattern.matcher("https://stackoverflow.com/questions/");
if (matcher.matches()) {
System.out.println(matcher.group("hostGroup"));
} else {
System.out.println("Not found! Invalid URL ^^");
}
The above will find stackoverflow.com for the following urls strings :
https://stackoverflow.com/questions/
http://stackoverflow.com/questions/
stackoverflow.com/questions/
stackoverflow.com:80/questions/
stackoverflow.com/
stackoverflow.com
I guess that's for practicing regex ? Otherwise, prefer using the standard APIs whenever possible - (in your case URI#getHost() !)
Cheers!

If you are sure that you are getting the proper URL format than you can just substring it preferred places.
public static void main(String[] args) {
String url = "https://stackoverflow.com/questions/";
System.out.println(getHostFast(url));
}
public static String getHostFast(String url) {
String subbed = url.substring(url.indexOf('/') + 2);
return subbed.substring(0, subbed.indexOf('/'));
}
The error prof method would need to contain additional check (for example if the next '/' exists after dropping http://.

Related

How to take query string from the request URL passed as string in java and ignore the rest of the URL

The URL "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd" is passed as a string to a Java file and from here I need to ignore till "?" and store the rest as a string in java.Can anyone help me with this.
Regex is a good option or you can also manipulate URLs using java.net.URL class:
URL url = new URL("http://" + "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd");
System.out.println(url.getPath()); // prints: /index.jsp
System.out.println(url.getQuery()); // prints: accountno=20&password=1234&username=abcd
Regex replacement would be one option here:
String url = "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd";
String query = url.replaceAll("^.*?(?:\\?|$)", "");
System.out.println(query);
This prints:
accountno=20&password=1234&username=abcd
String#substring
public class Main {
public static void main(String[] args) {
String url = "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd";
String paramsStr = url.substring(url.indexOf("?") + 1);
System.out.println(paramsStr);
}
}
Output:
accountno=20&password=1234&username=abcd
You can use .split()
String url = "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd";
String queryString = url.split("\\?")[1];

Java - Cut a string programmatically

I have a string (URL) like this:
"https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp"
I need to extract the last part only i.e. 02_Cuppy_lol.webp.
How can I do that?
Thanks!
You can use substring() and lastIndexOf() here:
String value = completeString.substring(completeString.lastIndexOf("/") + 1);
You can split this text/url and get last part, for example:
String url = "https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp";
String[] splittedUrl = url.split("/");
String lastPart = splittedUrl[splittedUrl.length()-1)];
you can use the method split().follow this example
public class Demo {
public static void main(String args[]){
String str ="https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp";
String[] temp=str.split("/");
int lastIndex =temp.length-1;
String lastPart = temp[lastIndex];
System.out.println(lastPart);
}
}
Output-:
02_Cuppy_lol.webp

phone Number validation in java

I want to validate a phone number in such Way :-
The field should allow the user to enter characters and should auto-correct. So an entry of "+1-908-528-5656" would not create an error for the user, it would just change to "19085285656".
I also want to number range between 9 to 11.
I also tried with the below code but not concluded to the final solution:
final String PHONE_REGEX = "^\\+([0-9\\-]?){9,11}[0-9]$";
final Pattern pattern = Pattern.compile(PHONE_REGEX);
String phone = "+1-908-528-5656";
phone=phone.replaceAll("[\\-\\+]", "");
System.out.println(phone);
final Matcher matcher = pattern.matcher(phone);
System.out.println(matcher.matches());
You can use simple String.matches(regex) to test any string against a regex pattern instead of using Pattern and Matcher classes.
Sample:
boolean isValid = phoneString.matches(regexPattern);
Find more examples
Here is the regex pattern as per your input string:
\+\d(-\d{3}){2}-\d{4}
Online demo
Better use Spring validation annotation for validation.
Example
// The Regex not validate mobile number, which is in internation format.
// The Following code work for me.
// I have use libphonenumber library to validate Number from below link.
// http://repo1.maven.org/maven2/com/googlecode/libphonenumber/libphonenumber/8.0.1/
// https://github.com/googlei18n/libphonenumber
// Here, is my source code.
public boolean isMobileNumberValid(String phoneNumber)
{
boolean isValid = false;
// Use the libphonenumber library to validate Number
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber swissNumberProto =null ;
try {
swissNumberProto = phoneUtil.parse(phoneNumber, "CH");
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
if(phoneUtil.isValidNumber(swissNumberProto))
{
isValid = true;
}
// The Library failed to validate number if it contains - sign
// thus use regex to validate Mobile Number.
String regex = "[0-9*#+() -]*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
Assuming your input field take any kind of character and you just want the digits.
String phone = "+1-908-528-5656";
phone=phone.replaceAll("[\\D]","");
if(phone.length()>=9 || phone.length()<=11)
System.out.println(phone);
We can use String.matches(String regex)1 to validate phone numbers using java.
Sample code snippet
package regex;
public class Phone {
private static boolean isValid(String s) {
String regex = "\\d{3}-\\d{3}-\\d{4}"; // XXX-XXX-XXXX
return s.matches(regex);
}
public static void main(String[] args) {
System.out.println(isValid("123-456-7890"));
}
}
P.S. The regex pattern we use extra '\' for escaping when we use in java string. (Try to use "\d{3}-\d{3}-\d{4}" in java program, you will get an error.
Assuming you want an optimization (which is what your comment suggests).
How bout this? (the "0" is to exclude if they give complete garbage without even a single digit).
int parse(String phone){
int num = Integer.parseInt("0"+phone.replaceAll("[^0-9]",""));
return 100000000<=num&&num<100000000000?num:-1;
}
I am not sure but removing the garbage characters parenthesis, spaces and hyphens, if you match with ^((\+[1-9]?[0-9])|0)?[7-9][0-9]{9}$ , you may validate a mobile number
private static final String PHONE_NUMBER_GARBAGE_REGEX = "[()\\s-]+";
private static final String PHONE_NUMBER_REGEX = "^((\\+[1-9]?[0-9])|0)?[7-9][0-9]{9}$";
private static final Pattern PHONE_NUMBER_PATTERN = Pattern.compile(PHONE_NUMBER_REGEX);
public static boolean validatePhoneNumber(String phoneNumber) {
return phoneNumber != null && PHONE_NUMBER_PATTERN.matcher(phoneNumber.replaceAll(PHONE_NUMBER_GARBAGE_REGEX, "")).matches();
}
One easy and simple to use java phone validation regex:
public static final String PHONE_VERIFICATION = "^[+0-9-\\(\\)\\s]*{6,14}$";
private static Pattern p;
private static Matcher m;
public static void main(String[] args)
{
//Phone validation
p = Pattern.compile(PHONE_VERIFICATION);
m = p.matcher("+1 212-788-8609");
boolean isPhoneValid = m.matches();
if(!isPhoneValid)
{
System.out.println("The Phone number is NOT valid!");
return;
}
System.out.println("The Phone number is valid!");
}
i have done testing one regex for this combination of phone numbers
(294) 784-4554
(247) 784 4554
(124)-784 4783
(124)-784-4783
(124) 784-4783
+1(202)555-0138
THIS REGEX SURELY WILL BE WORKING FOR ALL THE US NUMBERS
\d{10}|(?:\d{3}-){2}\d{4}|\(\d{3}\)\d{3}-?\d{4}|\(\d{3}\)-\d{3}-?\d{4}|\(\d{3}\) \d{3} ?\d{4}|\(\d{3}\)-\d{3} ?\d{4}|\(\d{3}\) \d{3}-?\d{4}
Building on #k_g's answers, but for US numbers.
static boolean isValidTelephoneNumber(String number) {
long num = Long.parseLong("0" + number.replaceAll("[^0-9]", ""));
return 2000000000L <= num && num < 19999999999L;
}
public static void main(String[] args) {
var numbers = List.of("+1 212-788-8609", "212-788-8609", "1 212-788-8609", "12127888609", "2127888609",
"7143788", "736103355");
numbers.forEach(number -> {
boolean isPhoneValid = isValidTelephoneNumber(number);
log.debug(number + " matches = " + isPhoneValid);
});
}

Regex Pattern matching for an URL

I have a method and input to that is an URL string. I would different types of URL (sample 5 URLs i have mentioned below)
String url1 = "http://domainName/contentid/controller_hscc.jsp?q=1" ;
String url2 = "http://domainName/contentid/controller.jsp?waitage=1" ;
String url3 = "http://domainName/contentid/controller_runner.jsp" ;
String url4 = "http://domainName/contentid/jump.jsp?q=5" ;
String url5 = "http://domainName/contentid/jump.jsp" ;
I need to find if this URL has controller*.jsp pattern in it. If so, I will have to write some other logic for it.
Now, I need to know how to write * controller*.jsp pattern in java
I wrote a regex this way and it returns false always
boolean retVal = Pattern.matches("^(controller)*", url) ;
PS : I use JDK1.4
EDIT-1
I tried below way. Still not woring
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloWorld{
public static void main(String []args){
String url = "http://domainName/contentid/controller_hscc.jsp?q=1" ;
//String url = "baaaaab" ;
String regex = "/controller(\\w+)?\\.jsp*" ;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(url);
System.out.println(m.matches());
}
}
Match it against the following regex:
controller(\w+)?\.jsp
Demo
Try this code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloWorld{
public static void main(String []args){
String url = "http://domainName/contentid/controller_hscc.jsp?q=1" ;
//String url = "baaaaab" ;
String regex = "^.*?controller(\\w+)?\\.jsp.*?$" ;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(url);
boolean retVal = Pattern.matches(regex, url) ;
System.out.println(m.matches() + "__" + retVal);
}
}
String re = "controller.+jsp";
String str = "http://domainName/contentid/controller_hscc.jsp?q=1";
Pattern p = Pattern.compile(re);
Matcher m = p.matcher(str);
I found this working fine:
(controller.*\.jsp(\?.*)?)
see here.
As a java string, it should be "(controller.*\\.jsp(\\?.*)?)"
Moreover, it will give you two groups: the whole one and the part after the ?.
You could also do it by splitting the url by /s, taking the last part and checking if that starts with controller - without using any regex.

Getting paramValue for paramName in specifed querystring using regex

I like to write a java utility method that returns paramValue for paramName in specified query string
Pattern p = Pattern.compile("\\&?(\\w+)\\= (I don't know what to put here) ");
public String getParamValue(String entireQueryString, String paramName)
{
Matcher m = p.matcher(entireQueryString);
while(m.find()) {
if(m.group(1).equals(paramName)) {
return m.group(2);
}
}
return null;
}
I will be invoking this method from my servlet,
String qs = request.getQueryString(); //action=initASDF&requestId=9078-32&redirect=http://www.mydomain.com?actionId=4343
System.out.println(getParamValue(qs, "requestId"));
The output should be, 9078-32
you can use a regex negated group. See this other SO question: Regular Expressions and negating a whole character group
You'll need to get everything except a &.
Use the proper API to do it: request.getParameter("requestId")
Could you split the string based on ampersands (&) and then search the resulting array for the key (look upto the equals sign).
Here's a link to String.split(): http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String%29
Here's the type of thing I'm talking about:
private static final String KEY_VALUE_SEPARATOR = "=";
private static final String QUERY_STRING_SEPARATOR = "&";
public String getParamValue(String entireQueryString, String paramName) {
String[] fragments = entireQueryString.split(QUERY_STRING_SEPARATOR);
for (String fragment : fragments){
if (fragment.substring(0, fragment.indexOf(KEY_VALUE_SEPARATOR)).equalsIgnoreCase(paramName)){
return fragment.substring(fragment.indexOf(KEY_VALUE_SEPARATOR)+1);
}
}
throw new RuntimeException("can't find value");
}
The Exception at the end is a pretty rubbish idea but that's not really the important part of this.

Categories

Resources