OH3: HTTP Binding + REGEX Help

I’m having problems getting REGEX to work with the HTTP Binding, i want to gather some information from my iFit profile which is publically available. When i don’t do any REGEX to the channel “elevation” i get full HTML code so i know its working.

I plugged my REGEX expression into https://regex101.com/ and it works so i’m at a loss. I read a bunch of threads and had no luck…

The OH3 HTTP Binding documentation states two colons and i tried both ways “REGEX:” and “REGEX::”

Here is what i have:

Thing:

Thing http:url:iFit_Alexa "Alexa iFit Elevation" [
    baseURL="https://www.ifit.com/profile/5febe06f7b394904ca3d07be/following",
    headers="key1=value1", "key2=value2", "key3=value3",
    stateMethod="GET",
    refresh=10    ] {
        Channels:
            Type string : elevation "Elevation" [ stateTransformation="REGEX:<span>(.*?) ft<" ]
    }

Item:

String iFit_Alexa_Elevation "Alexa Elevation [%s]" { channel="http:url:iFit_Alexa:elevation"}

The section of HTML i’m trying to capture is the Elevation of “2,213” in the HTML snip below:

>Monthly Stats</h2><ul class="stats"><li class="stat-item stat-item--elevation">Elevation<span>2,213 ft</span></li>

Does anyone see what i’m doing wrong?

I believe that in OH REGEX expressions still have to span the entire string even if you just want to capture a small section of it. See the second example:

So you just need a .* at the beginning and end of your pattern. Also I don’t think the ? is necessary your capture group as (.*) should be sufficient:

stateTransformation="REGEX:.*<span>(.*) ft<.*"
2 Likes

Thank you so much! I was trying everything and i guess i missed the .* and *. – Works perfectly!