简体   繁体   中英

Retrieving Regex matched pattern

I need to retrieve a regex pattern matched strings from the given input.

Lets say, the pattern I need to get is like,

"http://mysite.com/<somerandomvalues>/images/<againsomerandomvalues>.jpg"

Now I created the following regex pattern for this,

http:\/\/.*\.mysite\.com\/.*\/images\/.*\.jpg

Can anybody illustrate how to retrieve all the matched pattern with this regx expression using Java?

Some simple Java example:

String my_regex = "http://.*.mysite.com/.*/images/.*.jpg";
Pattern pattern = Pattern.compile(my_regex);
Matcher matcher = pattern.matcher(string_to_be_matched);
// Check all occurance
while (matcher.find()) {
    System.out.print("Start index: " + matcher.start());
    System.out.print(" End index: " + matcher.end() + " ");
    System.out.println(matcher.group());
}

You don't mask slashes but literal dots:

    String regex = "http://(.*)\\.mysite\\.com/(.*)/images/(.*)\\.jpg";
    String   url = "http://www.mysite.com/work/images/cat.jpg";
    Pattern pattern = Pattern.compile (regex);
    Matcher matcher = pattern.matcher (url);

    if (matcher.matches ())
    {
        int n = matcher.groupCount ();
        for (int i = 0; i <= n; ++i)
            System.out.println (matcher.group (i));
    }

Result:

www
work
cat

In fact, it is not clear if you want the whole matching string or only the groups.

Bogdan Emil Mariesan's answer can be reduced to

if ( matcher.matches () ) System.out.println(string_to_be_matched);

because you know it is mathed and there are no groups.

IMHO, user unknown's answer is correct if you want to get matched groups.

I just want to add additional information (for others) that if you need matched group you can use replaceFirst() method too:

String firstGroup = string.replaceFirst( "http://mysite.com/(.*)/images/", "$1" );

But performance of Pattern.compile approach if better if there are two or more groups or if you need to do that multiple times (on the other hand in programming contests, for example, it is faster to write replaceFirst() ).

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