It is currently December 12th, 2019, 8:21 pm

PCRE Regex Cheatsheet

General topics related to Rainmeter.
User avatar
CoffeeJoe
Posts: 17
Joined: November 6th, 2019, 4:19 am

PCRE Regex Cheatsheet

CoffeeJoe » November 21st, 2019, 3:51 pm

Throwing this out there because I'd never seen Regex before and couldn't find the pattern options I needed.
It may already be out there or it may be pinned somewhere I haven't seen yet... Either way

(Raw lift from this site)
https://www.debuggex.com/cheatsheet/regex/pcre

Regular Expression Basics
. Any character except newline
a The character a
ab The string ab
a|b a or b
a* 0 or more a's
\ Escapes a special character
Regular Expression Quantifiers
* 0 or more
+ 1 or more
? 0 or 1
{2} Exactly 2
{2, 5} Between 2 and 5
{2,} 2 or more
Default is greedy. Append ? for reluctant.
Regular Expression Groups
(...) Capturing group
(?P<Y>...) Capturing group named Y
(?:...) Non-capturing group
(?>...) Atomic group
(?|...) Duplicate group numbers
\Y Match the Y'th captured group
(?P=Y) Match the named group Y
(?R) Recurse into entire pattern
(?Y) Recurse into numbered group Y
(?&Y) Recurse into named group Y
\g{Y} Match the named or numbered group Y
\g<Y> Recurse into named or numbered group Y
(?#...) Comment

Regular Expression Character Classes
[ab-d] One character of: a, b, c, d
[^ab-d] One character except: a, b, c, d
[\b] Backspace character
\d One digit
\D One non-digit
\s One whitespace
\S One non-whitespace
\w One word character
\W One non-word character
Regular Expression Assertions
^ Start of string
\A Start of string, ignores m flag
$ End of string
\Z End of string, ignores m flag
\b Word boundary
\B Non-word boundary
\G Start of match
(?=...) Positive lookahead
(?!...) Negative lookahead
(?<=...) Positive lookbehind
(?<!...) Negative lookbehind
(?()|) Conditional
Regular Expression Escapes
\Q..\E Remove special meaning

Regular Expression Flags
i Ignore case
m ^ and $ match start and end of line
s . matches newline as well
x Allow spaces and comments
J Duplicate group names allowed
U Ungreedy quantifiers
(?iLmsux) Set flags within regex
Regular Expression Special Characters
\n Newline
\r Carriage return
\t Tab
\0 Null character
\YYY Octal character YYY
\xYY Hexadecimal character YY
\x{YY} Hexadecimeal character YY
\cY Control character Y
Regular Expression Posix Classes
[:alnum:] Letters and digits
[:alpha:] Letters
[:ascii:] Ascii codes 0 - 127
[:blank:] Space or tab only
[:cntrl:] Control characters
[:digit:] Decimal digits
[:graph:] Visible characters, except space
[:lower:] Lowercase letters
[:print:] Visible characters
[:punct:] Visible punctuation characters
[:space:] Whitespace
[:upper:] Uppercase letters
[:word:] Word characters
[:xdigit:] Hexadecimal digits
User avatar
jsmorley
Developer
Posts: 19864
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: PCRE Regex Cheatsheet

jsmorley » November 21st, 2019, 3:57 pm

User avatar
CoffeeJoe
Posts: 17
Joined: November 6th, 2019, 4:19 am

Re: PCRE Regex Cheatsheet

CoffeeJoe » November 21st, 2019, 4:39 pm

So like most of my adventures I found what I needed the hard way.
I meant it when I said, thank you for all you do - even if it seems insist on needlessly doing it myself :oops:
User avatar
Yamajac
Posts: 132
Joined: June 30th, 2014, 8:44 am

Re: PCRE Regex Cheatsheet

Yamajac » November 21st, 2019, 4:52 pm

Regex101.com is pretty awesome too.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 3643
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: PCRE Regex Cheatsheet

eclectic-tech » November 21st, 2019, 10:23 pm

Yamajac wrote:
November 21st, 2019, 4:52 pm
Regex101.com is pretty awesome too. :thumbup:
I'll second that... I have learned a lot by use RegEx101.com! 8-)
User avatar
jsmorley
Developer
Posts: 19864
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: PCRE Regex Cheatsheet

jsmorley » November 21st, 2019, 10:24 pm

You do not have the required permissions to view the files attached to this post.
User avatar
Jeff
Posts: 92
Joined: September 3rd, 2018, 11:18 am

Re: PCRE Regex Cheatsheet

Jeff » December 7th, 2019, 12:45 pm

Ah yes, REGEX101, totally doesn't have some exclusive features like \U or \L that don't work in RM
saying this because trying to do [:upper:] in regexp substitution doesn't work :(
User avatar
jsmorley
Developer
Posts: 19864
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: PCRE Regex Cheatsheet

jsmorley » December 7th, 2019, 12:49 pm

Jeff wrote:
December 7th, 2019, 12:45 pm
Ah yes, REGEX101, totally doesn't have some exclusive features like \U or \L that don't work in RM
saying this because trying to do [:upper:] in regexp substitution doesn't work :(
Yeah, it's unfortunate that those POSIX classes like [:lower:] don't work. Some of them would be pretty handy, as least as a perhaps more intuitive way of expressing some of those character classes. There are ways to replicate most of them using standard PCRE regular expression though.

https://www.regular-expressions.info/posixbrackets.html

So for instance Substitute="[:lower:]":"" could be Substitute="[a-z]":""

Or maybe for German: Substitute="[a-zäöü]":"". ;-)
User avatar
Yamajac
Posts: 132
Joined: June 30th, 2014, 8:44 am

Re: PCRE Regex Cheatsheet

Yamajac » December 7th, 2019, 7:54 pm

Jeff wrote:
December 7th, 2019, 12:45 pm
Ah yes, REGEX101, totally doesn't have some exclusive features like \U or \L that don't work in RM
saying this because trying to do [:upper:] in regexp substitution doesn't work :(
jsmorley wrote:
December 7th, 2019, 12:49 pm
Yeah, it's unfortunate that those POSIX classes like [:lower:] don't work. Some of them would be pretty handy, as least as a perhaps more intuitive way of expressing some of those character classes. There are ways to replicate most of them using standard PCRE regular expression though.

https://www.regular-expressions.info/posixbrackets.html

So for instance Substitute="[:lower:]":"" could be Substitute="[a-z]":""

Or maybe for German: Substitute="[a-zäöü]":"". ;-)
What are you guys on about?


Substitute = "[[:lower:]]" : "" works just fine lol.

Posix character classes are just 'characters' in and of themselves and can only be used inside a character class. It's basically shorthand that gets expanded.

In fact, if you punch your code in on Regex101 with the PCRE flavour selected, which should be the default, it'll even tell you this.

Image

Oh, and even the website JSMorley linked explains this.

Image
User avatar
jsmorley
Developer
Posts: 19864
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: PCRE Regex Cheatsheet

jsmorley » December 7th, 2019, 8:26 pm

Yamajac wrote:
December 7th, 2019, 7:54 pm
Substitute = "[[:lower:]]" : "" works just fine lol.
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.