It is currently February 25th, 2020, 9:14 am

Regular Expression in Rainmeter

Our most popular Tips and Tricks from the Rainmeter Team and others
User avatar
jsmorley
Developer
Posts: 20280
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Regular Expression in Rainmeter

Post by jsmorley »

The way I approach regular expression is:

Get the text I'm going to parse. That might be in something like RainRegExp.exe or maybe in my browser by using "View source" or maybe by using WebParser with Debug=2 set on the parent and Notepad++ to view WebParserDump.txt. Whatever it takes to get the actual text that I'm going to be looking to parse with WebParser, or RegExpSubstitute, or IfMatch.

I look at that text, and decide what values I'm trying to get out of it. Of course, presumably those values can "change" over time, as while you can use regular expression to just extract one bit of "static" data out of a boatload of other "static" text, the real value comes in the ability to extract a single bit of data that will "change", out of a boatload of other text.

I look at the context that value I'm trying to get "lives in" in the text. The question I ask is "what will uniquely get me to the start of the NEXT instance of that value, and what will tell me with certainty when the value ends?" While it is assumed that the "values" can change over time, it is also assumed that the "context" NEVER will change. If you can't define a consistent context, you can't use regular expression.

You MUST be able to say with certainty "what tells me when the value starts?" and "what tells me when the value ends?". That is the "context".

Then I start writing the regular expression. I use the tools that the language offers to define \d \w \s character type definitions, [character sets], and *+-{} quantifiers, mixing those in with the literal text that will zero me in on the value I'm trying to get. A decent cheat sheet is your friend in this...

https://docs.rainmeter.net/manual/skins/option-types/#RegExp
https://docs.rainmeter.net/tips/webparser-debugging-regexp/

Remember that ^ means "beginning of the string" and $ means "end of the string".

I use .* to "skip" lengths of text, and (.*) to "capture" the value(s) I want into StringIndex(es) if doing this for WebParser, or \1\2\3... capture groups for RegExpSubstitute. IfMatch generally doesn't need capture groups, it's just a "match" not a "capture".

https://docs.rainmeter.net/manual/measures/webparser/#RegExp
https://docs.rainmeter.net/manual/measures/general-options/substitute/#RegExpSubstitute
https://docs.rainmeter.net/manual/measures/general-options/ifmatchactions/

Whether in RainRegExp or regex101.com or even in the skin itself, test as you go. Each and every (capture) should return the single value you are interested in, and there is no point going forward if it doesn't return exactly that value, no more, no less. Trying to build an entire regular expression that (captures) multiple values all in one go will just be frustrating and hard to debug. Regular expression is very "linear", so it is easy to write, test, add more, test, add more, test and so on.
User avatar
Active Colors
Moderator
Posts: 538
Joined: February 16th, 2012, 3:32 am

Re: Regular Expression in Rainmeter

Post by Active Colors »

Thanks so much! I was hoping for such explanations for a while. Your explanations are very valuable!