It is currently March 28th, 2024, 7:56 pm

Little Help with RegExp / Search String showing in output

Get help with creating, editing & fixing problems with skins
austrini
Posts: 3
Joined: November 19th, 2018, 8:19 pm

Little Help with RegExp / Search String showing in output

Post by austrini »

I am new to this, and this seems like a simple question but i'm a little bit stumped.

The API for the website I'm pulling data from returns:
{"reviews_available":19,"next_review_date":1541692800,"reviews_available_next_hour":19,"reviews_available_next_day":19}}

I want to pull out "reviews_available" so

Code: Select all

[MeasureSite]
Measure=Webparser
URL=https://bunpro.jp/api/user/#APIKey#/study_queue
RegExp=(?siU)^(.*)$

[MeasureReviews]
Measure=Webparser
URL=[MeasureSite]
RegExp=(?siU)reviews_available":(.*),
If I run this through the webparser tester (which is great btw), it returns "19" which is what I want. If I actually load it through rainmeter, though, I get

Image

I can't figure out why it's displaying the entire search string. Any assistance is appreciated :/
I am using Rainmeter 4.2.0.3111, Windows 10.
Last edited by balala on November 19th, 2018, 8:44 pm, edited 1 time in total.
Reason: Please use Code tags when posting code snippets
User avatar
FreeRaider
Posts: 826
Joined: November 20th, 2012, 11:58 pm

Re: Little Help with RegExp / Search String showing in output

Post by FreeRaider »

Hard to say.

Try to add "next_review_date.* in your RegExp option.
austrini
Posts: 3
Joined: November 19th, 2018, 8:19 pm

Re: Little Help with RegExp / Search String showing in output

Post by austrini »

Thank you, I have tried that but it just appends whatever I put into the RegExp into the output.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Little Help with RegExp / Search String showing in output

Post by balala »

austrini wrote: November 19th, 2018, 8:27 pm I can't figure out why it's displaying the entire search string. Any assistance is appreciated :/
Although not forbidden, I wouldn't use a RegExp on the child WebParser measure, but would modify the RegExp of the [MeasureSite] (parent) measure, to get what I need.
So, replace the RegExp option of [MeasureParent] measure and remove the RegExp option of the [MeasureReviews] measure, as it follows:

Code: Select all

[MeasureSite]
Measure=WebParser
URL=https://bunpro.jp/api/user/#APIKey#/study_queue
RegExp=(?siU)"reviews_available":(.*),

[MeasureReviews]
Measure=WebParser
Url=[MeasureSite]
StringIndex=1
austrini
Posts: 3
Joined: November 19th, 2018, 8:19 pm

Re: Little Help with RegExp / Search String showing in output

Post by austrini »

That worked, thank you very much. I was going crazy trying to figure out why it was displaying the whole string.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Little Help with RegExp / Search String showing in output

Post by balala »

austrini wrote: November 19th, 2018, 9:03 pm That worked, thank you very much. I was going crazy trying to figure out why it was displaying the whole string.
I'm glad if you got it working.
Usually it's better to use one single RegExp option onto the parent measure, then extract the strings through some child measures, using the appropriate StringIndex values on them.
Obviously you can extend the RegExp expression and can add further child measures, to get more information, if needed.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Little Help with RegExp / Search String showing in output

Post by jsmorley »

While using a child measure is correct, in this case, with a single result being returned, the parent measure can in fact at once be both parent and child.

Code: Select all

[MeasureSite]
Measure=WebParser
URL=https://bunpro.jp/api/user/#APIKey#/study_queue
RegExp=(?siU)"reviews_available":(.*),
StringIndex=1
This would also work, without a specific, separate child measure.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Little Help with RegExp / Search String showing in output

Post by balala »

jsmorley wrote: November 19th, 2018, 10:29 pm While using a child measure is correct, in this case, with a single result being returned, the parent measure can in fact at once be both parent and child.

Code: Select all

[MeasureSite]
Measure=WebParser
URL=https://bunpro.jp/api/user/#APIKey#/study_queue
RegExp=(?siU)"reviews_available":(.*),
StringIndex=1
This would also work, without a specific, separate child measure.
Yes, but the question still remains: why in the original code (posted by austrini in the initial post of this topic) the [MeasureReviews] measure returns the whole reviews_available":19, string, not just the number? Because in the RegExp option of the [MeasureReviews] measure(.*) would mean just 19, not the whole reviews_available":19,.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Little Help with RegExp / Search String showing in output

Post by jsmorley »

balala wrote: November 20th, 2018, 10:27 am Yes, but the question still remains: why in the original code (posted by austrini in the initial post of this topic) the [MeasureReviews] measure returns the whole reviews_available":19, string, not just the number? Because in the RegExp option of the [MeasureReviews] measure(.*) would mean just 19, not the whole reviews_available":19,.

Code: Select all

[MeasureSite]
Measure=Webparser
URL=https://bunpro.jp/api/user/#APIKey#/study_queue
RegExp=(?siU)^(.*)$

[MeasureReviews]
Measure=Webparser
URL=[MeasureSite]
RegExp=(?siU)reviews_available":(.*),
If we leave WebParser out of this for a second, regular expression in and of itself is simply about "matching". A (capture group) doesn't cause the match to be any less or more than what is defined in the regular expression.

So in purely regular expression terms if we have the string:

{"reviews_available":19,"next_review_date":1541692800,"reviews_available_next_hour":19,"reviews_available_next_day":19}}

Then (?siU)^(.*)$ matches the entire string.

And (?siU)reviews_available":(.*), matches reviews_available":19,, and creates a (capture group) consisting of 19 with an index of 1.

With WebParser, it is the StringIndex on the child measure that returns the value of the indexed (capture group), or 19. Without a StringIndex, the entire match is returned.

That is the difference between a "parent" and "child" WebParser measure. A parent measure always returns the entire match, and creates (capture groups), indexed in the order they are in the regular expression. A child measure uses StringIndex to extract one of the indexed (capture groups) values from the parent, and returns just that. A WebParser measure cannot be a child without StringIndex.

Now as I said, a WebParser measure can be BOTH a "parent' and a "child" measure.

Code: Select all

[MeasureSite]
Measure=WebParser
URL=https://bunpro.jp/api/user/#APIKey#/study_queue
RegExp=(?siU)reviews_available":(.*),
StringIndex=1
It is the RegExp that defines the "match", which will be reviews_available":19,, and the StringIndex that extracts the single (capture group) that has a value of 19. Think of RegExp as the "parent" bit, and StringIndex as the "child" bit.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Little Help with RegExp / Search String showing in output

Post by balala »

jsmorley wrote: November 20th, 2018, 1:02 pm

Code: Select all

[MeasureSite]
Measure=WebParser
URL=https://bunpro.jp/api/user/#APIKey#/study_queue
RegExp=(?siU)reviews_available":(.*),
StringIndex=1
The above [MeasureReviews] measure returns reviews_available":19,, even if I add a StringIndex=1 option to [MeasureReviews]. With or without StringIndex, I get the same result.