It is currently March 29th, 2024, 12:08 pm

RegExp error handling - variable parsing code / missing strings = failing RegExp

Get help with creating, editing & fixing problems with skins
emp00
Posts: 78
Joined: October 7th, 2022, 8:08 pm

RegExp error handling - variable parsing code / missing strings = failing RegExp

Post by emp00 »

Dear Team,

I need to fix one "last" issue with my improved skin parsing the output of a certain api and displaying blood glucose values:

- My RegExp works fine for "my" cloud server api output (for those who are interested it's the "Nightscout pebble api")
- However, it should also work for other people who can have the server configured slightly differently
- Especially, some of the parsed strings might be missing for other users, see example as follows

Here's the problematic RegExp:

Code: Select all

RegExp=(?siU)now":(.*)}],.*sgv":"(.*)",.*trend":(.*),.*direction":"(.*)",.*datetime":(.*),.*bgdelta":(.*),.*iob":"(.*)",.*cob":(.*)}
; Output StringIndex
;	1 - Current Time
;	2 - Glucose
;	3 - Delta [Not used]
;	4 - Direction (Text)
;	5 - Glucose Timestamp
;	6 - BG Delta
;	7 - IOB
;	8 - COB
This works fine with my personal server's api output, looking for example like this (added linebreaks only for this posting):

{"status":[{"now":1667747473294}],"bgs":
[{"sgv":"131","trend":4,"direction":"Flat","datetime":1667747202471,
"bgdelta":5,"battery":"96","iob":"6.86","bwp":"-5.11","bwpo":-88,"cob":57.8}],"cals":[]}


However, here's the api output of a different user/server as another example (again, linebreaks added for comparison):

{"status":[{"now":1668113492344}],"bgs":
[{"sgv":"117","trend":4,"direction":"Flat","datetime":1668113241000,
"bgdelta":2}],"cals":[]}


As you can see, the strings can vary in order (which is no problem for the RegExp) but also certain strings can be missing completely, because the other user/server does not need or does not have these specific options. In this example strings 7+8 called "IOB" and "COB" are missing. Therefore, my RexExp and the skin simply fails to display any data. I got feedback from users that my skin "does not work" and I could now prove this with the RainRegExp tool: The variable api output is obviously the reason behind.

Now my question: How can I make the above RexExp work (and not fail) for situations when certain strings are missing? In such cases I would like the RegExp to return all existing string values and simply leave the missing strings empty, in this case it should return "" for IOB and COB. Hopefully there is some error handling functionality telling Rainmeter to ignore missing RegExp strings? Could not find this in the documentation yet.

Thanks so much for your help (again)!!
emp00
Posts: 78
Joined: October 7th, 2022, 8:08 pm

Re: RegExp error handling - variable parsing code / missing strings = failing RegExp

Post by emp00 »

Hmm, seems to be difficult.

My problem is exactly this RegExp error: Error = 1: Could not match all searches.

I did some more research and found this WebParser option: LogSubstringErrors=0
Does this solve my problem? Unfortunately I cannot test this with RenRegExp ...
emp00
Posts: 78
Joined: October 7th, 2022, 8:08 pm

Re: RegExp error handling - variable parsing code / missing strings = failing RegExp

Post by emp00 »

Yeah!! I think I did it "all by myself" :-) --> found "look-ahead assertions" in the Rainmeter documentation here:
https://docs.rainmeter.net/tips/webparser-lookahead-assertions-in-regexp/

Working solution for my problem in case anybody's interested:

RegExp=(?siU)(?(?=.*now":).*now":(.*)[,"}])(?(?=.*sgv":").*sgv":"(.*)[,"}])(?(?=.*trend":).*trend":(.*)[,"}])(?(?=.*direction":").*direction":"(.*)[,"}])(?(?=.*datetime":).*datetime":(.*)[,"}])(?(?=.*bgdelta":).*bgdelta":(.*)[,"}])(?(?=.*iob":").*iob":"(.*)[,"}])(?(?=.*cob":).*cob":(.*)[,"}])

Now the RexExp returns the desired strings and does NOT fail anymore if some items are missing. In such cases these strings are returned "blank" which is exactly what I was looking for. WTF - the RegExp cannot be read/understood by normal humans anymore but "the machine" now does what it's supposed to do. Yeaaaaaaah!

:great: :twisted: :welcome: