It is currently November 18th, 2018, 11:42 pm

webparser regexp display matching strings only

Help with creating, editing & fixing problems with skins
AnimaliX
Posts: 9
Joined: August 22nd, 2016, 9:22 pm

webparser regexp display matching strings only

AnimaliX » November 19th, 2017, 9:30 am

hello.
I cant find here on forum what I need to do from my rainmeter :)

I need that webparser find and display lines with matched strings only...
for example I need display content between <title>(.*)</title> which matching strings in this case words: "Alpha" or "Beta" or "Gamma" (Alpha|Beta|Gamma) (case insesitive)
how to write proper regexp? :) I have only classic simple rss parser like this

Code: Select all

RegExp="(?siU).*<item>.*<title>(.*)</title>.*
thx
User avatar
FreeRaider
Posts: 752
Joined: November 20th, 2012, 11:58 pm

Re: webparser regexp display matching strings only

FreeRaider » November 19th, 2017, 10:43 am

AnimaliX
Posts: 9
Joined: August 22nd, 2016, 9:22 pm

Re: webparser regexp display matching strings only

AnimaliX » November 19th, 2017, 10:57 am

thanks for info, but this is not it I believe... this not skip "no match" lines I think
well this "IfMatchActions" is something new, so maybe I'm wrong :)

for example
rss source:
1. ..<title>text ABC text</title>..
2. ..<title>text ABC text</title>..
3. ..<title>text Alpha text</title>..
4. ..<title>text ABC text</title>..
5. ..<title>text Beta text</title>..

in result I need display only lines 3 and 5 and exclude 1,2,4
User avatar
balala
Rainmeter Sage
Posts: 7014
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: webparser regexp display matching strings only

balala » November 19th, 2017, 1:47 pm

AnimaliX wrote:for example
rss source:
1. ..<title>text ABC text</title>..
2. ..<title>text ABC text</title>..
3. ..<title>text Alpha text</title>..
4. ..<title>text ABC text</title>..
5. ..<title>text Beta text</title>..

in result I need display only lines 3 and 5 and exclude 1,2,4
Not exactly the best solution, but the Lookahead Assertion could somehow help, I think.
Eg for the above source, you should try the following RegExp: RegExp=(?siU)<title>.*(?(?=Alpha|Beta|Gamma).*(.*))</title>.*<title>.*(?(?=Alpha|Beta|Gamma).*(.*))</title>.*<title>.*(?(?=Alpha|Beta|Gamma).*(.*))</title>.*<title>.*(?(?=Alpha|Beta|Gamma).*(.*))</title>.*<title>.*(?(?=Alpha|Beta|Gamma).*(.*))</title>. The only problem is that the returned string will contain the Alpha, Beta or Gamma words along with the following strings/words, but not those placed before them. For the posted source the third child WebParser measure (which has set StringIndex=3) returns Alpha text, the last one (with StringIndex=5) returns Beta text and the first, the second and the fourth don't return anything (in fact they are returning empty strings). As you can see the text word before the Alpha text and Beta text, is missing.
Is this a problem?
User avatar
FreeRaider
Posts: 752
Joined: November 20th, 2012, 11:58 pm

Re: webparser regexp display matching strings only

FreeRaider » November 19th, 2017, 4:22 pm

I think I have another solution. It uses a lua script.

title.txt

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
	<title>RSS Title</title>
	<description>This is an example of an RSS feed</description>
	<link>http://www.someexamplerssdomain.com/main.html</link>
	<lastBuildDate>Mon, 06 Sep 2009 16:45:00 +0000 </lastBuildDate>
	<item>
		<title>Text ABC 01 Text</title>
		<description>Here is some text containing an interesting description 01.</description>
		<link>http://www.wikipedia.org/</link>
		<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
	</item>
	<item>
		<title>Text ABC</title>
		<description>Here is some text containing an interesting description ABC.</description>
		<link>http://www.wikipedia.org/</link>
		<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
	</item>
	<item>
		<title>Text Alpha 01 Text</title>
		<description>Here is some text containing an interesting description Alpha 01.</description>
		<link>http://www.wikipedia.org/</link>
		<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
	</item>
	<item>
		<title>BeTa 03 Text</title>
		<description>Here is some text containing an interesting description BeTa 03.</description>
		<link>http://www.wikipedia.org/</link>
		<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
	</item>
	<item>
		<title>GAMMA 31</title>
		<description>Here is some text containing an interesting description GAMMA 31.</description>
		<link>http://www.wikipedia.org/</link>
		<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
	</item>
	<item>
		<title>Text abc 01</title>
		<description>Here is some text containing an interesting description Text abc 01.</description>
		<link>http://www.wikipedia.org/</link>
		<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
	</item>
</channel>
</rss>
rss.ini

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize=1

[MeasureRSS]
Measure=Plugin
Plugin=WebParser
URL=file://#CURRENTPATH#title.txt
RegExp=(?siU)^(.*)$
UpdateRate=600
FinishAction=[!UpdateMeasure MeasureScript]

[MeasureScript]
Measure=Script
ScriptFile=title.lua
Match1=alpha
Match2=beta
Match3=gamma
UpdateDivider=-1

[MeterName1]
Meter=String
X=0
Y=0
[MeterName2]
Meter=String
X=0
Y=0R
[MeterName3]
Meter=String
X=0
Y=0R
[MeterName4]
Meter=String
X=0
Y=0R
[MeterName5]
Meter=String
X=0
Y=0R
[MeterName6]
Meter=String
X=0
Y=0R
[MeterName7]
Meter=String
X=0
Y=0R
title.lua

Code: Select all

function Initialize()
	
   measureSite = SKIN:GetMeasure('MeasureRSS')
   stype1 = SELF:GetOption('Match1')
   stype2 = SELF:GetOption('Match2')
   stype3 = SELF:GetOption('Match3')
   titlePattern = '<title>(.-)</title>'
   itemPattern = '<item>.-</item>'
   	
end

function Update()

   allData = measureSite:GetStringValue()
   if allData == '' then return end

   dummyString, itemCount = string.gsub(allData, '<item', '')

   startPos = 0
   
   for i = 1, itemCount do
      
      currentItemStart, currentItemEnd = string.find(allData, itemPattern, startPos)
      currentItem = string.sub(allData, currentItemStart, currentItemEnd)
      
	  current_title = string.match(currentItem, titlePattern)
	  
	  if string.match(nocase(current_title), stype1) then
		SKIN:Bang('!SetOption', 'MeterName'..i, 'Text', current_title)
	elseif string.match(nocase(current_title), stype2) then
		SKIN:Bang('!SetOption', 'MeterName'..i, 'Text', current_title)
	elseif string.match(nocase(current_title), stype3) then
		SKIN:Bang('!SetOption', 'MeterName'..i, 'Text', current_title)	
		
	  
	  end
      
      startPos = currentItemEnd + 1
	        
   end
   
   SKIN:Bang('!ShowMeterGroup', 'Meters')
   
   return itemCount
  

end

function nocase (s)
      s = string.gsub(s, "%a", function (c)
            return string.format("%s", string.lower(c),
                                           string.upper(c))
          end)
      return s
    end
In this case it returns in order

Text Alpha 01 Text
BeTa 03 Text
GAMMA 31

the order depends on what you type in match1, match2 and match3.


I hope this helps you