I have a the follwing equation
Can i make this line working for all doorsensors ?
if item.name == 'doorsensor_tuindeur_1' then -- kerui wifi devices kent open en dicht, generiek maken voor alle kerui's
<rest of the code>
something like:
if item.name == 'doorsensor*' then
wildcard in string [Solved]
Moderator: leecollings
-
pvklink
- Posts: 822
- Joined: Wednesday 12 November 2014 15:01
- Target OS: Raspberry Pi / ODroid
- Domoticz version: latest b
- Contact:
wildcard in string
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
Jablotron connection, Ikea
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: wildcard in string
Wildcards are handled a bit different in Lua then you might expect if you are used to other languagespvklink wrote: Saturday 26 October 2019 19:17 I have a the follwing equation. Can i make this line working for all doorsensors ?
if item.name == 'doorsensor_tuindeur_1' then -- kerui wifi devices kent open en dicht, generiek maken voor alle kerui's
<rest of the code>
You could use something like below function. It handles two types of wildcard chars
'*' wildcard for 0 or more chars.
'?' wildcard for 1 char
Code: Select all
local function matchesWildCardedString(stringToMatch, wildCardedString)
local magicalChars = '[^%w%s~{\\}:&(/)<>,?@#|_^*$]' -- Lua 'magical chars'
local regExpression = ('^' .. wildCardedString:gsub("[%^$]",".")) -- make it a valid regexpression
if wildCardedString:find('*') and wildCardedString:find('?') then
dz.log('Not possible to combine single char wildcard(s) "?" with multi char wildcard(s) "*"',dz.LOG_ERROR)
return false
elseif wildCardedString:find('*') then -- a * wild-card was used
wildCardedString = (regExpression:gsub("*", ".*") .. '$'):gsub(magicalChars,'.') -- replace * with .* (Lua for zero or more of any char)
elseif wildCardedString:find('?') then -- a ? wild-card was used
wildCardedString = (regExpression:gsub("?", ".") .. '$'):gsub(magicalChars,'.') -- replace ? with . (Lua for single any char)
end
return stringToMatch:match(wildCardedString) == stringToMatch
end
if matchesWildCardedString(item.name, 'doorsensor*') thenDebian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
pvklink
- Posts: 822
- Joined: Wednesday 12 November 2014 15:01
- Target OS: Raspberry Pi / ODroid
- Domoticz version: latest b
- Contact:
Re: wildcard in string
wow, i thought this would be easy!
thanks!
Do i use it like this:
if matchesWildCardedString(item.name,'doorsensor*) then OR
if matchesWildCardedString(item.name,'doorsensor_tuindeur_?) then
thanks!
Do i use it like this:
if matchesWildCardedString(item.name,'doorsensor*) then OR
if matchesWildCardedString(item.name,'doorsensor_tuindeur_?) then
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
Jablotron connection, Ikea
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: wildcard in string [Solved]
You can use either one (if you add the closing quotes) sopvklink wrote: Saturday 26 October 2019 21:18 Do i use it like this:
if matchesWildCardedString(item.name,'doorsensor*) then OR
if matchesWildCardedString(item.name,'doorsensor_tuindeur_?) then
Code: Select all
if matchesWildCardedString(item.name,'doorsensor*') then Code: Select all
if matchesWildCardedString(item.name,'doorsensor_tuindeur_?') thenDebian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
pvklink
- Posts: 822
- Joined: Wednesday 12 November 2014 15:01
- Target OS: Raspberry Pi / ODroid
- Domoticz version: latest b
- Contact:
Re: wildcard in string
Works great!
I placed it in global_data.lua
I placed it in global_data.lua
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
Jablotron connection, Ikea
Who is online
Users browsing this forum: Google [Bot] and 1 guest