Help with Regex Placeholder

Is the string always exactly the same pattern?

CCCC CCCCCC CC

Four characters, a space, six characters, a space and then two characters.

If so something like the following should work:

\w{4}\s(\w{6})\s\w{2}

\w means “word character” so should match numbers and letters but not special characters or white space. {x} means “match the previous x times”. \s means white space.

The stuff in parens is what get’s returned (the six characters after the first four characters).

In openHAB, you must match all of the String and tell it what part you want with the parens. This is different from “standard” regex and is largely why we see “It works on regex101.com but not in openHAB”.

OK, so there are not spaces in the String than. No problem, just remove the \s from the above.

Depending on what you are doing with these, Design Pattern: Encoding and Accessing Values in Rules Approach 3 might be useful to you.

2 Likes