How to use a switch statement with regular expressions in Java?

Jan 16, 2026

Leave a message

Yo! I'm a switch supplier, and today I'm gonna talk about how to use a switch statement with regular expressions in Java. It might sound a bit techy, but don't worry, I'll break it down for you.

First off, what's a switch statement in Java? Well, it's a control structure that allows you to select one of many code blocks to execute based on the value of an expression. For example, you might use a switch statement to handle different cases based on the day of the week.

int day = 3;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    // and so on...
    default:
        System.out.println("Invalid day");
}

But what if we want to use regular expressions in a switch statement? Regular expressions are patterns used to match character combinations in strings. They're super useful when you need to handle different types of text inputs.

In Java, prior to Java 14, using regular expressions directly in a switch statement wasn't straightforward. But starting from Java 14, we got some cool new features like switch expressions and pattern matching, which make this task a lot easier.

Let's say we have a string that represents different types of switch names, and we want to perform different actions based on the pattern of the name. Here's how we can do it:

import java.util.regex.Pattern;

public class SwitchRegexExample {
    public static void main(String[] args) {
        String switchName = "6GK5008 - 0BA10 - 1AB2 Electrical Switch Scalance XB008";
        // We'll define some regex patterns
        Pattern pattern1 = Pattern.compile("^6GK\\d+-\\w+-\\w+");
        Pattern pattern2 = Pattern.compile("^D4A-\\d+N");
        Pattern pattern3 = Pattern.compile("^514120");

        if (pattern1.matcher(switchName).find()) {
            switch (switchName) {
                case var name when pattern1.matcher(name).matches():
                    System.out.println("Matched a pattern similar to 6GK...");
                    // You can include more actions here
                    System.out.println("Check out this related switch: [6GK5008-0BA10-1AB2 Electrical Switch Scalance XB008](/industrial-electronic-parts/switch/6gk5008-0ba10-1ab2-electrical-switch.html)");
                    break;
                default:
                    System.out.println("No match");
            }
        } else if (pattern2.matcher(switchName).find()) {
            switch (switchName) {
                case var name when pattern2.matcher(name).matches():
                    System.out.println("Matched a pattern similar to D4A...");
                    System.out.println("Here's a relevant switch: [D4A - 4501N D4A - 4510N Limit Switch](/industrial-electronic-parts/switch/d4a-4501n-d4a-4510n-limit-switch.html)");
                    break;
                default:
                    System.out.println("No match");
            }
        } else if (pattern3.matcher(switchName).find()) {
            switch (switchName) {
                case var name when pattern3.matcher(name).matches():
                    System.out.println("Matched a pattern similar to 514120...");
                    System.out.println("Take a look at this actuator: [514120 Actuator](/industrial-electronic-parts/switch/514120-actuator.html)");
                    break;
                default:
                    System.out.println("No match");
            }
        }
    }
}

In this code, we first define three regular expression patterns. Then, we check if the switchName string matches each pattern. If it does, we use a switch statement to handle the matching case. Inside the matching case, we print a message and provide a link to a relevant product.

Now, let's break down the code a bit more. The Pattern.compile() method is used to create a Pattern object from a regular expression string. The Matcher class is then used to perform the actual matching against a given input string. The matches() method checks if the entire input string matches the pattern, while the find() method checks if any part of the input string matches the pattern.

The case var name when ... syntax is a new feature in Java 14 and later. It allows us to use a pattern matching condition within a switch case. This makes it easier to write clean and readable code when dealing with complex conditions.

Using regular expressions in a switch statement can be really handy in scenarios where you have a large number of possible input values, and you want to group them based on certain patterns. For example, in a system that manages different types of electrical switches, you might have names that follow specific naming conventions. By using regular expressions, you can easily categorize and handle these switches based on their names.

If you're in the market for switches or related products, you know we've got a wide range to offer. Whether you're looking for the 6GK5008-0BA10-1AB2 Electrical Switch Scalance XB008, the D4A - 4501N D4A - 4510N Limit Switch, or the 514120 Actuator, we're here to assist you. If you're interested in making a purchase or just want to have a chat about your requirements, don't hesitate to reach out. We're always ready to have a good discussion and help you find the right products for your needs.

References

514120 Actuator For 514120 Safety Switch6GK5008-0BA10-1AB2 Electrical Switch Scalance XB008

  • Java Documentation
  • Regular Expressions Tutorials

Send Inquiry