Closed frankdevhub closed 5 years ago
Take a look at this: https://regex101.com/r/nOtNC1/1
See Group 1 matches "www.frankdevhub.sites"
Is that what you want?
class Main {
public static void main(String[] args) {
// Examaple string is testUrl, it matches but has remain characters(so the regex is not functionally correct)
String urlRegx = "(^([wW]{3}.|[wW][aA][pP].|[fF][tT][pP].|[fF][iI][lL][eE].)([-A-Za-z0-9#_]+.)([-A-Za-z0-9#_]+))[/|&](([-A-Za-z0-9+&@#/%?=~_|!:;]+)?)$";
Pattern pattern = Pattern.compile(urlRegx);
String testUrl = "www.frankdevhub.sites/parm1=foo&&parm2=foo";
Matcher matcher = pattern.matcher(testUrl);
boolean flag = false;
if (matcher.find()) {
String find = matcher.group(1);
System.out.print(String.format("[find:]%s", find));
if (testUrl.equals(find))
flag = true;
}
assert (flag); //exceptions occurs here because after match the source string still remains something
}
}
Notice I changed the regex and
String find = matcher.group();
to String find = matcher.group(1);
@jeffslofish Thanks a lot! Will check and study the API method.
Best Regards!
@ulx @ulxshop @ulxsms @ulxsw @xiaotianzhang1990 @cpovirk @ningyu1 @jeffslofish
Hi jeffslofish, may i ask for your help?
Hi WU ZE HAO, are u there?
关于正则表达式使用相关:
如何使得表达式可以匹配但是一次匹配后不留剩下的(源字符串应该是匹配格式的子集)
Hi , using regex to match a url string which format should be like as follows (no protocol tag like
https
)Issure
How to use a correct regex to match a format but doesnt have extra characters after using match method?
For example you match a string like "abc_rest....", you need "abc" it is matched but it still contains rest characters.
So the problem is how make sure the regex only match single example in the string you need to deal with?
Details
How to resolve ???
I tried sometimes and when using regex like
^([the regex format])$
it may help, so i want to make sure is it the right way to make sure the string not only can be matched but also doesn't has any other remained characters after doing match method like:Format Example
Thanks !