REGEX help on extracting string

Beginner need some help on REGEX

My items are named as RoomName+"_ " + Item name. Eg:

Janka_CurrTemp (g_CurrTemp)
Janka_TempSetpoint (g_TempSetpoint)

When I try to trigger a rule if CurrTemp of any room changes it is working well and I can write out the name of triggering room to v_szoba:

when
Member of g_CurrTemp changed
then
val v_szoba = transform(“REGEX”, “(.*?)_CurrTemp”, triggeringItem.name)
logInfo( “Teszt2”, v_szoba + " " + triggeringItem.name)

2020-11-01 14:53:38.508 [INFO ] [clipse.smarthome.model.script.Teszt2] - Janka Janka_CurrTemp

But if I would trigger the same rule by changes of more items so I remove the item “post fix” (eg CurrTemp) from the REGEX it come back with Null value in the v_szoba.

when
Member of g_CurrTemp changed or
Member of g_TempSetpoint changed
then
val v_szoba = transform(“REGEX”, “(.*?)_”, triggeringItem.name)
logInfo( “Teszt2”, v_szoba + " " + triggeringItem.name)

2020-11-01 14:53:38.582 [INFO ] [clipse.smarthome.model.script.Teszt2] - null Janka_CurrTemp

If I check it on Regex101 the result should be the same:

searching for “CurrTemp":
image
searching for "
” only:
image

Thanks for any hint

This regex is a full string comparison. (.*?)_ doesn’t result in any matches because your pattern ends in an underscore, but your string does not. If you add one more wildcard to the end of your pattern (.*?)_.*, it will be able to match all your items.

1 Like

Thnaks a lot. Works well :slight_smile: