It is currently May 7th, 2021, 2:04 pm

请帮忙, 关于 regexp

General topics related to Rainmeter.
User avatar
goeway
Posts: 21
Joined: March 20th, 2019, 8:50 pm

请帮忙, 关于 regexp

Post by goeway »

I got a string of data through WebParser and then used a dumb method to get all the values of the "Funds",

Data :

Code: Select all

([{"Date":"02-02","Funds":12491},{"Date":"02-01","Funds":12458},{"Date":"01-29","Funds":12423},{"Date":"01-28","Funds":12398},{"Date":"01-27","Funds":12462},{"Date":"01-26","Funds":12474},{"Date":"01-25","Funds":12510},{"Date":"01-22","Funds":12491},{"Date":"01-21","Funds":12511},{"Date":"01-20","Funds":12454},{"Date":"01-19","Funds":12420},{"Date":"01-18","Funds":12411},{"Date":"01-15","Funds":12394},{"Date":"01-14","Funds":12386},{"Date":"01-13","Funds":12335},{"Date":"01-12","Funds":12308},{"Date":"01-11","Funds":12224},{"Date":"01-08","Funds":12215},{"Date":"01-07","Funds":12009},{"Date":"01-06","Funds":11976}])
RegExp:

Code: Select all

RegExp=(?siU).*,"Funds":(.*)},.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}.*,"Funds":(.*)}
how can I simplify this line of code?
User avatar
balala
Rainmeter Sage
Posts: 12636
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: 请帮忙, 关于 regexp

Post by balala »

goeway wrote: February 3rd, 2021, 4:38 am how can I simplify this line of code?
Actually too much you can't, however what I usually do in such cases is to create a variable (usually name it Item, or something similar) and use that one in the RegExp expression.
If you are interested, add the following variable to the [Variables] section of your code: Item=\{"Date":".*","Funds":(.*)}. Note that I slightly modified your expression, which you've used into yourRegExp.
Replace the RegExp option with the following one: RegExp=(?siU)#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#. If you do this, you can later extremely easily add further or remove #Item# elements.
Also note that if the number of the {"Date":"xxxx","Funds":xxxxx} elements is varying, you can use Lookahead Assertions in the RegExp, in order to get it working no matter of number of such expressions in the html code.
User avatar
goeway
Posts: 21
Joined: March 20th, 2019, 8:50 pm

Re: *, * regexp

Post by goeway »

balala wrote: February 3rd, 2021, 7:03 am Actually too much you can't, however what I usually do in such cases is to create a variable (usually name it Item, or something similar) and use that one in the RegExp expression.
If you are interested, add the following variable to the [Variables] section of your code: Item=\{"Date":".*","Funds":(.*)}. Note that I slightly modified your expression, which you've used into yourRegExp.
Replace the RegExp option with the following one: RegExp=(?siU)#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#,#Item#. If you do this, you can later extremely easily add further or remove #Item# elements.
Also note that if the number of the {"Date":"xxxx","Funds":xxxxx} elements is varying, you can use Lookahead Assertions in the RegExp, in order to get it working no matter of number of such expressions in the html code.
Thank you for your reply and help. The method you mentioned I have used on some skin. :rosegift:
User avatar
balala
Rainmeter Sage
Posts: 12636
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Re: *, * regexp

Post by balala »

goeway wrote: February 3rd, 2021, 9:15 am Thank you for your reply and help. The method you mentioned I have used on some skin. :rosegift:
Glad to help. :thumbup: