Regex rule text within brackets removal lo

Tags: #<Tag:0x00007efec4ce8cd0> #<Tag:0x00007efec4ce8c08> #<Tag:0x00007efec4ce8b18>

So I’m trying to write a rule for a virtual switch that will (eventually) tell me over the sink audio what songs are playing on active squeezebox players.

Right now i have it checking one playrr, eventuly I’ll need to do a group and check if the players are actively playing.

I’m having an issue with a regex for album title where I need to strip out brackets and whatever is within the brackets. I’m alittle rusty with regex but this is throwing me for a loop within OH2 and keeps returning NULL. Here’s my rule so far:

rule "announce" when                                                              
        Item SBAnnounce changed then {                                            
if (SBAnnounce.state == ON) {                                                     
                                                                                  
val String  = transform("REGEX", "igiveupremovebrackets", SBPRottonPi_Album.state.toString)
                                                                                  
/*                                                                                
val String = SBPRottonPi_Album.state                                              
*/                                                                                
        var linetosay = String::format("The current song playing is " + SBRottonPi_Artist + "on the album" + String) 
        say(linetosay, "voicerss:enUS")                                           
                                                                                  
        SBAnnounce.postUpdate(OFF)                                                
                                                                                  
/*           this is now going through chromecast binding so it plays full                                                                     
SBPGoogleHome_Player.sendCommand(OFF)                                             
*/                                                                                
        }                                                                         
}                                                                                 
end

Sorry for the messy code… Been doing it mostly via ssh from phone :smile:

The trick with regex in OH is the regex must match the full string and only the parts in parens gets returned.

If the stuff in brackets is at the beginning or end there is no problem.

\[.*\](.*)

Or

(.)[.]

Will work. If the stuff in brackets is in the middle you will need to use a Rule with two separate regex, one to get the stuff before and another to h get the stuff after.

(.*)\[.*\].*
.*\[.*\](.*)

If you have the case where the square brackets only appear sometimes you will need to use a Rule and test whether they are present before running the steering through the regex.

Continuing the discussion from Regex rule text within brackets removal lo:

(.*)\[.*\].*
.*\[.*\](.*)

If you have the case where the square brackets only appear sometimes you will need to use a Rule and test whether they are present before running the steering through the regex.

Yes, so it turns out I needed to change to \\[ in order for it to run correctly, and my tags can have multiple brackets like “[Remastered]” before and after the actual album title. Then in some cases, I don’t have any and the regex would fail, so with my lack of programming I wound up getting it all working, but not the way I’d hope writing having to write 2 additional variations of the same line (I’m not explaining this well but there’s alot going on where I am) .

rule "announce" when
        Item SBAnnounce changed then {

val String  = String::format("" + SBPRottonPi_Album.state)

if (SBAnnounce.state == ON) {


if (transform("REGEX", "(.*)\\[.*\\].*", String) === null) {
        logInfo("FILE",String)
        val linetosay = String::format("The current song playing is " + SBPRottonPi_Title.state + " from " + SBPRottonPi_Artist.state + " on " + String + " released in " + SBPRottonPi_Year.state + ".")
        say(linetosay,"voicerss:enUS")

} else {
var String2  = transform("REGEX", "(.*)\\[.*\\].*", String.toString)
var String3  = transform("REGEX", ".*\\[.*\\](.*)", String2.toString)
        logInfo("FILE",String3)
        val linetosay = String::format("The current song playing is " + SBPRottonPi_Title.state + " from " + SBPRottonPi_Artist.state + " on " + String3 + " released in " + SBPRottonPi_Year.state + ".")
        say(linetosay,"voicerss:enUS")

}

        SBAnnounce.postUpdate(OFF)

/*
SBPGoogleHome_Player.sendCommand(OFF)
*/
        }
}
end