简体   繁体   中英

Java regEx URL matching issue

and as usual thank you in advance.

I am trying to familiarize myself with regEx and I am having an issue matching a URL.

Here is an example URL:

www.examplesite.com/dir/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html

here is what my regex breakdown looks like:

[site]/[dir]*?/[year]/[month]/[day]/[storyTitle]?/[id]/htmlpage.html

the [id] is a string 22 characters in length that can be either uppercase or lowercase letters, as well as numbers. However, I do not want to extract that from the URL. Just clarifying

Now, I need to extract two values from this url.

First, I need to extract the dirs(s). However, the [dir] is optional, but also can be as many as wanted. In other words that parameter could not be there, or it could be dir1/dir2/dir3 ..etc . So, going off my first example :

    www.examplesite.com/dir1/dir2/dir3/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html

Here I would need to extract dir1/dir2/dir3 where a dir is a string that is a single word with all lowercase letters (ie sports/mlb/games). There are no numbers in the dir, only using that as an example.

But in this example of a valid URL:

www.examplesite.com/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html

There is no [dir] so I would not extract anything. thus, the [dir] is optional

Secondly, I need to extract the [storyTitle] where the [storyTitle] is also optional just like the [dir] above, but however if there is a storyTitle there can only be one.

So going off my previous examples

www.examplesite.com/dir/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html

would be valid where I need to extract 'title-of-some-story' where story titles are dash separated strings that are always lowercase. The example belowis also valid:

www.examplesite.com/dir/2012/06/19/FAQKZjC3veXSalP9zxFgZP/htmlpage.html

In the above example, there is no [storyTitle] thus making it optional

Lastly, just to be thorough, a URL without a [dir] and without a [storyTitle] are also valid. Example:

www.examplesite.com/2012/06/19/FAQKZjC3veXSalP9zxFgZP/htmlpage.html

Is a valid URL. Any input would be helpful I hope I am clear.

Here is one example that will work.

public static void main(String[] args) {

    Pattern p = Pattern.compile("(?:http://)?.+?(/.+?)?/\\d+/\\d{2}/\\d{2}(/.+?)?/\\w{22}");

    String[] strings ={
            "www.examplesite.com/dir1/dir2/4444/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html",
            "www.examplesite.com/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html",
            "www.examplesite.com/dir/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html",
            "www.examplesite.com/dir/2012/06/19/FAQKZjC3veXSalP9zxFgZP/htmlpage.html",
            "www.examplesite.com/2012/06/19/FAQKZjC3veXSalP9zxFgZP/htmlpage.html"
    };
    for (int idx = 0; idx < strings.length; idx++) {
        Matcher m = p.matcher(strings[idx]);
        if (m.find()) {
            String dir = m.group(1);
            String title = m.group(2);
            if (title != null) {
                title = title.substring(1); // remove the leading /
            }
            System.out.println(idx+": Dir: "+dir+", Title: "+title);
        }
    }
}

Here is an all regex solution.

Edit: Allows for http://

Java source:

import java.util.*;
import java.lang.*;
import java.util.regex.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String url = "http://www.examplesite.com/dir/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html";
        String url2 = "www.examplesite.com/dir/dir2/dir3/2012/06/19/FAQKZjC3veXSalP9zxFgZP/htmlpage.html";
        String url3 = "www.examplesite.com/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html";

        String patternStr = "(?:http://)?[^/]*[/]?([\\S]*)/[\\d]{4}/[\\d]{2}/[\\d]{2}[/]?([\\S]*)/[\\S]*/[\\S]*";

        // Compile regular expression
        Pattern pattern = Pattern.compile(patternStr);


        // Match 1st url
        System.out.println("Match 1st URL:");
        Matcher matcher = pattern.matcher(url);

        if (matcher.find()) {
            System.out.println("URL: " + matcher.group(0));
            System.out.println("DIR: " + matcher.group(1));
            System.out.println("TITLE: " + matcher.group(2));
        }
        else{ System.out.println("No match."); }


        // Match 2nd url
        System.out.println("\nMatch 2nd URL:");
        matcher = pattern.matcher(url2);

        if (matcher.find()) {
            System.out.println("URL: " + matcher.group(0));
            System.out.println("DIR: " + matcher.group(1));
            System.out.println("TITLE: " + matcher.group(2));
        }
        else{ System.out.println("No match."); }


        // Match 3rd url
        System.out.println("\nMatch 3rd URL:");
        matcher = pattern.matcher(url3);

        if (matcher.find()) {
            System.out.println("URL: " + matcher.group(0));
            System.out.println("DIR: " + matcher.group(1));
            System.out.println("TITLE: " + matcher.group(2));
        }
        else{ System.out.println("No match."); }
    }
}

Output:

Match 1st URL:
URL: http://www.examplesite.com/dir/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html
DIR: dir
TITLE: title-of-some-story

Match 2nd URL:
URL: www.examplesite.com/dir/dir2/dir3/2012/06/19/FAQKZjC3veXSalP9zxFgZP/htmlpage.html
DIR: dir/dir2/dir3
TITLE: 

Match 3rd URL:
URL: www.examplesite.com/2012/06/19/title-of-some-story/FAQKZjC3veXSalP9zxFgZP/htmlpage.html
DIR: 
TITLE: title-of-some-story

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM