It is currently October 23rd, 2020, 11:04 am

PCRE Regex Cheatsheet

General topics related to Rainmeter.
User avatar
Yamajac
Posts: 134
Joined: June 30th, 2014, 8:44 am

Re: PCRE Regex Cheatsheet

Post by Yamajac »

jsmorley wrote: December 7th, 2019, 8:26 pm So it does... I'd never really played with it before, and didn't tumble to the fact that is in effect a [:class:] within a [class]. Nice.
I also don't really use them since [A-Z] is just easier to type than [[:upper:]] anyway lol. Not really what you want from a shorthand lol.

Posix character classes aren't normal bracket expressions, they're a different thing that are used in the same way because hey lets make things even more confusing?
User avatar
Jeff
Posts: 160
Joined: September 3rd, 2018, 11:18 am

Re: PCRE Regex Cheatsheet

Post by Jeff »

regexp substitution

"(.)(.)":"\U$1\E$2"

this won't work in rainmeter

for measures the easy way to overcome it is to lua it (unless you take the time to make 26+ substitutions so it's native), for meters the easy way is to inline pattern it
User avatar
Yamajac
Posts: 134
Joined: June 30th, 2014, 8:44 am

Re: PCRE Regex Cheatsheet

Post by Yamajac »

Jeff wrote: December 8th, 2019, 12:11 am regexp substitution

"(.)(.)":"\U$1\E$2"

this won't work in rainmeter

for measures the easy way to overcome it is to lua it (unless you take the time to make 26+ substitutions so it's native), for meters the easy way is to inline pattern it
Yea, unfortunately there's no way to do that, that's a regex101 exclusive thing. IDK why they made an exclusive thing for a regex tester but hey.

[:upper:] definitely does work though.
User avatar
Active Colors
Moderator
Posts: 618
Joined: February 16th, 2012, 3:32 am

Re: PCRE Regex Cheatsheet

Post by Active Colors »

All those regular expression tools online want me to insert my regular expression first. Are there any tools that help to come up with regular expression at the first place?

I have such a string:

Code: Select all

<div class="buyItemEdition secondaryText">
            edition of 11&nbsp;
            <span class="notable end">8 remaining</span>
 </div>
I need to get two number values. One is edition of X and another is Y remaining. Can I feed my html code to some tool and get the X and Y indicated somehow?
User avatar
jsmorley
Developer
Posts: 21424
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: PCRE Regex Cheatsheet

Post by jsmorley »

If what you are saying is that you would like to paste in something like <item>11</item> and then ask it "what is the regular expression I need to get the "11" out of this?" then no, I don't know of a tool that does that offhand. There may be something, just have never run into it. Every regular expression tester I have seen is more "did I do this right?" than "how do I do this?".


1.png


https://regex101.com/ is probably the best tool out there online.

RainRegExp works pretty well too, and has the advantages that 1) It can connect to and download the actual site you are trying to parse, and 2) It is automatically very "Rainmeter-centric", in that it uses the same PCRE library and global flags that Rainmeter uses, and is focused on (StringIndex) "capture groups" rather than "matches". The downside of RainRegExp is that it doesn't give you the very cool and extremely useful "explanation" of what is happening and why that regex101.com does. If your goal is to "learn" how regular expression works, then regex101.com is a great tool.
You do not have the required permissions to view the files attached to this post.
User avatar
Yamajac
Posts: 134
Joined: June 30th, 2014, 8:44 am

Re: PCRE Regex Cheatsheet

Post by Yamajac »

For smaller strings, txt2re works reasonably well.


For your example, it spits out .*?(\d+) which works fine in a webparser measure, but wouldn't work in a string measure as is. Though for smaller strings it's faster to just write a regex that works anyway.