0

I have a String for a routing key:

"key modifier path.to.routing.key;"
  • "key" is always present (I don't need to store this part)
  • "modifier" (a word) can appear 0 or more times (I need to store the modifiers)
  • "path.to.routing.key" Is the actual key and will always be the last component of the line followed by a ";" There may be 0 or more '.' characters in the routing key but always at least 1 word (I need to store this part).

The string will be on its own line in a file with other non interesting text [so I cant just use String.split(" ");]. I plan to use regex Patterns to get it out using:

Pattern p = Pattern.compile("...pattern...");

Im new to java regex and could do with some help,

How can I use Pattern to seperate this String into its components?


Some examples if it helps:

key public direct routing.key;
key direct routingkey;
4
  • Wait, is the question how to use regex to find the line in the file or how to use regex to split up the line into its components that you have to store? If the latter - why use regex? Commented Sep 20, 2014 at 14:17
  • 1
    "The string will be on its own line in a file with other non interesting text [so I cant just use String.split(" ");]" This I find contradicting. Why can't you just split on " " if the string is in its own line of text? Commented Sep 20, 2014 at 14:19
  • @Fildor A bit of both, I'm using regex as the non interesting text in the rest of the file may contain the word key etc Commented Sep 20, 2014 at 14:20
  • In that case, I'd use a regex to identify the line, and just split the line without using regex. Good old divide and conquer :) Commented Sep 20, 2014 at 14:21

3 Answers 3

3

Use s. Here I capture your modifiers and the key, see this regex match:

^key (\w+(?: \w+)*) ([\w.]++);$
MATCH 1: [Group 1: public direct] [Group 2: routing.key]
MATCH 2: [Group 1: direct] [Group 2: routingkey]

Here is a regex demo.

You can then use .split(" ") to split the modifiers.


As code:

Pattern pattern = Pattern.compile("^key (\\w+(?: \\w+)*) ([\\w.]++);$", Pattern.MULTILINE);
Matcher matcher = pattern.matcher("key public direct routing.key;\nkey direct routingkey;");
while (matcher.find()) {
    for (final String modifier : matcher.group(1).split(" "))
        System.out.println(modifier);
    System.out.println(matcher.group(2));
}

Here is an online code demo!

Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

key(?: (.*))? (.+);

See the Debuggex flowchart to understand how it works. The modifiers will be in group 1, and the routing key will be in group 2. You can use MatchResult.group(number)) to extract a group from a match result.

Regular expression visualization

Debuggex Demo

Comments

-1

Here a simple sample of what you want:

public static void main(String[] args) {
    Pattern pattern = Pattern.compile("key (?:(.*) )?(.*?);");
    // key, facultative things (group 1), mandatory thing (group 2)

    String[] tests = new String[]{"key public direct routing.key;", "key direct routingkey;", "key nomodifier;"};

    for (String test : tests) {
        System.out.println("test: " + test);

        Matcher matcher = pattern.matcher(test);
        if (matcher.matches()) {
            // split facultative modifiers
            String[] modifiers = matcher.group(1) == null ? new String[0] : matcher.group(1).split(" ");

            // retrieve the mandatory key
            String key = matcher.group(2);

            System.out.println("modifiers: " + Arrays.toString(modifiers) + "; key: " + key);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.