frankdevhub / frankdevhub.github.io

Blog template powered by jekyll to share the interesting stories in life (基于Jekyll的模板引擎的维基风格的个人博客,记录分享生活中的新鲜故事)
http://blog.frankdevhub.site
MIT License
5 stars 3 forks source link

how to get regex match and make sure no extra format after match like type [[match][rest...]] ? #31

Closed frankdevhub closed 5 years ago

frankdevhub commented 5 years ago

@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

    // 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-z09#_]+)[/|&](([-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();
            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

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:

 // if matched then
 matcher.group(); // first match match result should be something
 matcher.group(); // second match should have nothing 

Format Example

Thanks !

jeffslofish commented 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?

jeffslofish commented 5 years ago
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

  }
}
jeffslofish commented 5 years ago

Notice I changed the regex and String find = matcher.group(); to String find = matcher.group(1);

frankdevhub commented 5 years ago

@jeffslofish Thanks a lot! Will check and study the API method.

Best Regards!