I am really new to regex. I try to search and also try some similar solution in every stackoverflow but no luck to find the solution that I really need. I need help to check the URL that needs contain at least one forward slash ('/') in addition to the protocol part (e.g. 'https://') of the url. Meaning it needs to be a forward slash after the domain part of the URL prefix.
for example:
- https://someurl.com/ <- valid
- http://www.url.gg/ <- valid
- http://sample.com/some-api/edit <- valid
- https://test <- not valid
- https://www.test.com <- not valid
Thanks in advance
CodePudding user response:
You can match an additional slash after the protocol and a wild card:
^https?://.*/
Demo: https://regex101.com/r/9KGMGO/1
CodePudding user response:
Pattern : '^https://([a-z,.]*)/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^https://([a-z,.]*)/", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("https://test");
boolean matchFound = matcher.find();
if(matchFound) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
}
Do upvote the solution, if it helps :)