RegExp in Rainmeter skin issue

1.6k Views Asked by At

I'm creating a rainmeter skin, where I need to read the content of a local file and then split up its content.

The content of my local file looks something like this:

sentence 1
sentence 2
sentence 3
sentence 4

and my Rainmeter webparser measure looks like this:

[MeasureParser]
Measure=Plugin
Plugin=WebParser.dll
Url=File://#SKINSPATH#\Notes\Files\notes.txt
RegExp="((.*)((?>\r\n|\n|\r)?))+" 

And then my individual string indexers would have looked like

[MeasureParserChild1]
Measure=Plugin
Plugin=WebParser.dll
Url=[MeasureParser]
StringIndex=1

What I would have wanted it to do is split the file content by newline (?>\r\n|\n|\r), but apparently it doesn't work for me. The way I read my regex is like that there is a number of characters (.*) followed by a possible newline (?>\r\n|\n|\r)? that is optional (in case there is only one line in my file. And then all that (.*)((?>\r\n|\n|\r)?) can be present several times (1 or more).

Anyways it's not working so I have a flaw in my thinking some place, and I hope someone can help me find it please.

Big thank you in advance to any possible help. :)

1

There are 1 best solutions below

1
On

First of all, you cannot simply quantify your whole pattern, and hope to reference each match with an index, regular expressions doesn't work like that, it will gobble up every possible match, and return the last one.

Secondly, because you've set the line content to zero-or-more and the newline part to optional, your last match is likely to always be an empty string.

What you'll really want is to create a variable that holds your pattern for matching a single line, something like ^(.+)$ should suffice, as it will use the anchors ^ and $ to match beginning and end of a string. Then you'll want to use that variable as many times as you want lines returned.

If you want 5 lines to be read from your file, you'll do something like RegExp=(?m)#SingleLine##SingleLine##SingleLine##SingleLine##SingleLine#. The modifier (?m) is there to ensure ^ and $ match beginning and end of a line, instead of trying to match the beginning and end of the entire file. You can then reference the lines with index 1 through 5.

If you want something more dynamic, where you don't have to repeat the pattern, and have it automatically scale with the size of your file you'll probably have to resort to some Lua scripting.