It is currently May 4th, 2024, 5:31 am

Parsing a local txt

Get help with creating, editing & fixing problems with skins
Oreoz
Posts: 5
Joined: February 27th, 2011, 12:37 am

Parsing a local txt

Post by Oreoz »

Hello,

I am having a little trouble understanding why my parser is not working. (And i know it might be something silly i've missed)

I am trying to make it read from a local text file that has this text inside:

CS@OK
S1@OK
S2@OK
S3@OK
UO@100

This is my code for rainmeter:

Code: Select all


; ----------------------------------

[Rainmeter]
; This section contains general settings that can be used to change how Rainmeter behaves.
Author=anon
AppVersion=1003000
Update=1000

[Metadata]
; Contains basic information of the skin.
Name=untitled
Description=This skin displays basic system stats.
License=Creative Commons BY-NC-SA 3.0
Version=1.0.0

[Variables]
URL="C:\Users\Admin\Documents\Rainmeter\Skins\illustro\Serv\serverstats.txt" 
fontName=Trebuchet MS
FontColor=255, 255, 255, 255


;===================MEASURES=================

[MeasureSERSTAT] 
Measure=Plugin 
Plugin=Plugins\WebParser.dll 
UpdateRate=1800 
Url=#URL#
RegExp="(?siU)@(.+);(.+)@(.+);(.+)@(.+);(.+)@(.+);(.+)@(.+);(.+)@(.+);(.+)@(.+);"

[MeasureCS] 
Measure=Plugin 
Plugin=Plugins\WebParser.dll 
UpdateRate=1800 
Url=[MeasureSERSTAT]
StringIndex=3


;=================METERS======================

[MeterBackground]
Meter=IMAGE
X=1
Y=1
H=220
W=300
SolidColor=0,0,0,255

[MeterSTATUS] 
MeasureName=MeasureCS
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[MeasureCS]
I have tried changing the string indices, but that seems to do absolutely nothing.
If my regexp is wrong, would someone be kind enough to help me along by explaining exactly where I am going wrong and what steps I should take to make it work?

Thanks.
User avatar
Scolex
Posts: 111
Joined: July 31st, 2010, 8:52 am

Re: Parsing a local txt

Post by Scolex »

This might help.
http://rainmeter.net/cms/Tips-WebParserPrimer

Posting an actual example of the file you are parsing and what you want from it would help us help you.

You have (.+) in your regexp I have never seen that normally it is (.*) for the data you want returned.
User avatar
jsmorley
Developer
Posts: 22631
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing a local txt

Post by jsmorley »

Since the only "delimiter" between the values you want to get is a linefeed, you are going to need to be sure each line has one, including the last line. So whatever is writing this text file needs to write "value \n" or some such, depending on what is creating it. If the last entry just ends the file, without a linefeed on it as well, you may never get the last entry.

Having said that, you will need to use a linefeed char (\n) to indicate where each "capture" should end, and a "look ahead assertion" to enable a variable number of entries in the file without the Regular Expression failing:

RegExp="(?siU)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)"

This will capture the values after the "@" symbol and before the linefeed up to and including 10 times, but won't fail if for instance there are only 5. More info on "look around" HERE.

You also MUST use a protocol value on the URL= to tell WebParser which internet protocol (http:// ftp:// or in your case file://) to use. I added it to the front of the URL= valuie.

Code: Select all

; ----------------------------------

[Rainmeter]
; This section contains general settings that can be used to change how Rainmeter behaves.
Author=anon
AppVersion=1003000
Update=1000

[Metadata]
; Contains basic information of the skin.
Name=untitled
Description=This skin displays basic system stats.
License=Creative Commons BY-NC-SA 3.0
Version=1.0.0

[Variables]
URL="file://C:\Users\Admin\Documents\Rainmeter\Skins\illustro\Serv\serverstats.txt"
fontName=Trebuchet MS
FontColor=255, 255, 255, 255

;===================MEASURES=================

[MeasureSERSTAT]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateRate=1800
Url=#URL#
RegExp="(?siU)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)(?(?=.*@).*@(.*)\n)"

[MeasureCS]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateRate=1800
Url=[MeasureSERSTAT]
StringIndex=1

;=================METERS======================

[MeterBackground]
Meter=IMAGE
X=1
Y=1
H=220
W=300
SolidColor=0,0,0,255

[MeterSTATUS]
Meter=String
MeasureName=MeasureCS
FontColor=255,255,255,255
Oreoz
Posts: 5
Joined: February 27th, 2011, 12:37 am

Re: Parsing a local txt

Post by Oreoz »

Thank you for your help.
I have tried using the script you have provided, but so far it did not work.
I will see if there is something else that's wrong.

Log file comes back with -

WebParser: [MeasureSERSTAT] PCRE compilation failed at offset 99: missing
User avatar
jsmorley
Developer
Posts: 22631
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing a local txt

Post by jsmorley »

It works for me, as I always test what I post. Maybe you can zip up and attach the actual serverstats.txt file, so I can be sure what is actually in it. That trailing linefeed on the final line is important. Also, are you sure you are logged in as a user called "Admin"?
Oreoz
Posts: 5
Joined: February 27th, 2011, 12:37 am

Re: Parsing a local txt

Post by Oreoz »

To answer your question - yes, because i am the sole user of the pc. and i have all the admin rights.

Please find attached the text file that I am trying to parse.

Again, many thanks for your help thus far.
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 22631
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing a local txt

Post by jsmorley »

Ah, while your original post was a DAMNED LIE (grin) since you didn't have the semi-colons on your example of serverstats.txt, this actually makes it easier since you now have a decent delimiter.

Should work fine if you change the RegExp to:

RegExp="(?siU)(?(?=.*@).*@(.*);)(?(?=.*@).*@(.*);)(?(?=.*@).*@(.*);)(?(?=.*@).*@(.*);)(?(?=.*@).*@(.*);)(?(?=.*@).*@(.*);)(?(?=.*@).*@(.*);)(?(?=.*@).*@(.*);)(?(?=.*@).*@(.*);)"
Oreoz
Posts: 5
Joined: February 27th, 2011, 12:37 am

Re: Parsing a local txt

Post by Oreoz »

Crap..I forgot to add those as i typed it.. sorry!

And it works! haha. thanks so much.
Now i will have to go through the regex so i can fully get how the damn thing works (or how I can get it to work for me, next time without pestering anyone else).

Thanks again!
User avatar
jsmorley
Developer
Posts: 22631
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing a local txt

Post by jsmorley »

Glad to help.
User avatar
jsmorley
Developer
Posts: 22631
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing a local txt

Post by jsmorley »

Look ahead in Regular Expression is not the most user friendly thing out there.

The long and the short of it is:

(? ......... )

Means check to see if whatever "........" is replaced with succeeds, if not, just skip it and do the next bit.

Inside of that, we have:

(?=.*@)

This basically means "is there a pattern ".*@" coming up? if yes, then the (? ....... ) is executed, which is going to look for the actual pattern and do a "capture".

So if you put the two together, you get:

(?(?=.*@).*@(.*);)

And it says; If the test for .*@ succeeds, then we know an "@" is indeed coming up, so search for it and return everything after it but before the ";" char into a capture / StringIndex. If not, don't fail entirely, but just go on to the next expression or gracefully end if it is the last. This "look ahead" stuff is important if you are not sure how many "items" are going to be in your source. Without this, if you look for 5 and there are 4, the entire thing fails.