Regex groups capture and consumption

Following the guide you linked, I thought I had to use a "=~" find operator in order to create a Matcher instance:

data.each {
  switch (it.key) {
   
  case =~/humidity([1-8])/:
    model = "wh31_ch${Matcher.lastMatcher.group(1)}";
    break;
  }
}

But Hubitat is flagging that as invalid operator.
Still not sure how to consume regex groups in a switch/case statement... :thinking:

1 Like

Very much disagree. Until computers can fix bugs themselves it is humans that need to read code. I refuse to hire a developer that hasn't read https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

The best developers are the ones that can write code that ANYONE can understand. The worst developers are the ones that write code that other developers can't understand.

:thinking: Give me a sec... Wrapping my head around what you are trying to do..

You can't use =~ in a case like that. That is an assignment operator to set a variable.

Amen, although I would tweak that a little to "The best developers are the ones that can write code that ANY OTHER DEVELOPER can understand.".

But I agree in spirit.

That said, I'm not a great developer, and use regex occasionally. lol

1 Like

I know. I thought somehow an hidden "Matcher" instance would be created.

I haven't tried doing this but it is just standard groovy. Check this SO article, Groovy Regex: Capture group in Switch Statement - Stack Overflow

:face_with_raised_eyebrow: Lack of understanding of Regex does not mean code is unreadable.. Just unreadable by someone who doesn't understand regex (which is well documented) .. Just as writing code in C is not understandable to people who don't know C.. Beauty behind regex is it supported by many languages and is quite a useful tool

1 Like

That was exactly my source. By doing that I alway get a null java exception raised.

I haven't used it in the context of what you are trying to do.. I have always used the output of regex.. But still thinking it through..

There are many times I see devs using regex for SIMPLE comparitors, which is much less readable than simple english - in those specific cases. The fact that you need a regex testing tool to begin with reinforces the point (again, in those cases).

That said, a good regex (maybe with an associated descriptive comment on function :slight_smile: ) can compact quite a list of matching code into a single line, which can be very nice as well.

Give me a sample input and an expected output and I can help

Found this old SmartThings post. Perhaps it's still the case in Hubitat... :confused:

What you are looking for is a truth validation..

The following is valid code:

data.each {
    switch (it.key) {

    case ~/humidity([1-8])/:
        model = "wh31_ch${Matcher.lastMatcher.group(1)}";
        break;
    }
}

What you need to do is put your text in regex101 and your regex code and see what it returns:

regexCapture

1 Like

So the actual cause of your problem is you didn't import the namespace. model = "wh31_ch${java.util.regex.Matcher.lastMatcher.group(1)}"; works just fine.

I did log.debug java.util.regex.Matcher.lastMatcher and it shows

java.util.regex.Matcher[pattern=humidity([1-8]) region=0,9 lastmatch=humidity1]

3 Likes
String key = "humidity1"

switch (key) {
   
case ~/humidity([1-8])/:
  channel = Matcher.lastMatcher.group(1);  // channel should be "1"
  break;
}

However the above code always raises a null exception in Hubitat

sorry .. it's not /gm it's /g (default in groovy)
regexCapture4

Duh, of course... thank you! :pray::pray:

2 Likes

Yep ... missed that you were trying to use the match.. I was focused on the switch case validation..

1 Like

Again thank you @dman2306, @bcopeland, @codahq, etc. Super helpful community!

I updated my Ecowitt driver and it's now working beautifully.

Regarding regex in general, IMHO they have their own usage case. When used appropriately they are a thing of beauty. When abused they are ugly beasts.

As in most other things in life it's important to use the right tool for the job.

4 Likes