Matching specific URL slug patterns with Java regular expressions

When working with URL slugs, it’s often necessary to match and validate them against specific patterns. Regular expressions (regex) can be a powerful tool to achieve this in Java. In this blog post, we will explore how to use Java regular expressions to match specific URL slug patterns.

What is a URL slug?

A URL slug is a user-friendly representation of a webpage’s title in the URL. For example, consider the following URL: https://www.example.com/blog/post-slug-example. Here, post-slug-example is the URL slug representing the title of the blog post.

Matching URL slugs with regular expressions

To match specific URL slug patterns using Java regular expressions, we can utilize the java.util.regex package. Let’s consider some common URL slug patterns and see how we can match them.

Pattern 1: Alphanumeric slugs

Alphanumeric slugs contain only lowercase letters, numbers, and hyphens. To match this pattern, we can use the following regular expression:

String pattern = "^[a-z0-9-]+$";

This regex pattern consists of the following components:

Pattern 2: Slugs with predefined words

Sometimes, we may want to match slugs that contain specific predefined words or phrases. For example, consider the following URL: https://www.example.com/blog/javascript-frameworks-2021. Here, we want to match the slug javascript-frameworks-2021, which contains the predefined word javascript.

To match slugs with predefined words, we can use the following regular expression:

String pattern = ".*javascript.*";

This regex pattern consists of the following components:

Using the regular expressions in Java

To match the URL slug patterns using the regular expressions defined above, we can utilize the java.util.regex.Pattern and java.util.regex.Matcher classes in Java. Here’s an example usage:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SlugMatcher {
    public static void main(String[] args) {
        String urlSlug = "post-slug-example";

        // Pattern for alphanumeric slugs
        String pattern1 = "^[a-z0-9-]+$";

        // Pattern for slugs with predefined words
        String pattern2 = ".*example.*";

        // Matching pattern 1
        Pattern regex1 = Pattern.compile(pattern1);
        Matcher matcher1 = regex1.matcher(urlSlug);
        boolean isPattern1Matched = matcher1.matches();
        
        // Matching pattern 2
        Pattern regex2 = Pattern.compile(pattern2);
        Matcher matcher2 = regex2.matcher(urlSlug);
        boolean isPattern2Matched = matcher2.matches();

        System.out.println("Pattern 1 matched: " + isPattern1Matched);
        System.out.println("Pattern 2 matched: " + isPattern2Matched);
    }
}

Make sure to replace urlSlug with the actual URL slug you want to match. The matches() method of Matcher class returns true if the slug matches the pattern, and false otherwise.

Conclusion

Matching specific URL slug patterns can be achieved using Java regular expressions. By leveraging regex patterns, we can validate and match slugs against various criteria. In this blog post, we learned how to match alphanumeric slugs and slugs with predefined words. Feel free to customize and expand upon these patterns as per your specific needs.

#Java #Regex