📚
YAGPDB
  • YAGPDB Coding System - Documentation
  • Getting Started
  • Useful Functions
  • Primary Level
    • The Dot and Variables
      • User
      • Guild / Server
      • Member
      • Channel
      • Message
      • Reaction
      • Time
    • Custom Types
    • Functions
      • Type Conversion
      • String Manipulation
      • Math Functions
      • Message Functions
      • Mentions
      • Role Functions
      • Current User
      • Miscellaneous
      • ExecCC
    • Conditional Branching
  • Intermediate Level
  • Database
  • Range Action
  • With Action
  • Custom Embeds
  • Advanced Level
    • Using RegEx
  • Custom Commands Examples
    • Texas Hold'em
Powered by GitBook
On this page
  • Preface
  • Regex
  • Composites
  • Repetitions
  • Grouping
  • Flags
  • Empty Strings
  • Escape Sequences
  • Character Class Elements
  • Named Character Classes As Character Class Elements
  • Perl Character Classes
  • ASCII Character Classes

Was this helpful?

  1. Advanced Level

Using RegEx

What does RegEx mean? This is short for "regular expression". Being 3x harder than other parts, it defeats many people, but you can make good use of it and become a pro so long as you keep trying it

Preface

Now you want to make a command that will only be triggered when the user sends the specific kind of links, what can you do? I think Using Regex will be a good way to solve this problem.

To clarify more, if you want to make a command that can be triggered when you send a discord message link, you can use https://(ptb.|canary.)?discord(?:app)?.com/channels\/(\d+)\/(\d+)\/(\d+) as the trigger and set the command type to Regex. Only when your message contains a link of a message will the command be triggered.

Let's see another example. If you want the command to be triggered when your message contains a number or more, you can use \d+ to do it.

If you are the admin of a server, you may be faced with a problem like the following one: if your members keep using -cc <CCID>, but you don't want to set the command override, what can you do? Just go to automodv2 and make a new rule using the Message matches regex as the trigger type and type in (?i)\A(-|<@!?204255221017214977>)\s*(cc|customcommands)\s+153 so that no one can check the codes of that command (cc 153) anymore.

Here are the codes of the commands mentioned above.

Matches the message link:
{{$matches := reFindAllSubmatches `https://(ptb.|canary.)?discord(?:app)?.com/channels\/(\d+)\/(\d+)\/(\d+)` .Message.Content}}
Show the objects matching the provided regex: {{$matches}}
-----------------------------------------------------------
Matches any number one or more time:
{{$matches := reFindAll `\d+` .Message.Content}}
{{$matches}}
-----------------------------------------------------------
The command will be triggered when you type "-cc 153" in the begining of a message:
{{$matches := reFind `(?i)\A(-|<@!?204255221017214977>)\s*(cc|customcommands)\s+153` .Message.Content}}
{{$matches}}

Regex

A regex is a string of text that allows you to create patterns that help match, locate, and manage text. When first trying to understand regular expressions it seems as if it is a different language. However, mastering regular expressions can save you thousands of hours if you work with text or need to parse large amounts of data. Below is an example of a regular expression with each of its components labeled.

Composites

AB, or A|B? You want both, or one of the options?

Usage

Composites

xy

x followed by y

x|y

x or y (prefer x)

Character

Usage

Input

Matches

|

abc|ab|a

(1)adb

(2)cadd

(3)f

(4)abca

(1)[[a]]

(2)[[a]]

(3)[]

|

abc |t t

(1)abc

(2)abc t

(3)abct t t t

(4)tt

(5)abc

(1)[]

(4)[]

Explain More:

  • [[abc] [a]]: The length of this is 2 because it matches two things.

  • [[abc ]]: The length of this is 1 because it only matches one thing.

  • [[t t] [t t]]: The length of this is 2 because it only matches t t two times. Although it has more than 2 spaces, the length of the $matches depends on how many times it matches.

  • []: It matches nothing because there isn't anything behind the "abc ". But when you type "abc t", it will match [[abc ]].

Repetitions

Only match 1, 2, 999, more, or even infinite characters? In a specific range or something else?

Usage

Repetitions

x*

zero or more x, prefer more

x+

one or more x, prefer more

x?

zero or one x, prefer one

x{n,m}

n or n+1 or ... or m x, prefer more

x{n,}

n or more x, prefer more

x{n}

exactly n x

Character

Usage

Input

Matches

{x}

a{2}

(1)a

(2)aa

(3)aaa

(4)abaa (5)aaaa

(1)[]

(2)[[aa]]

(3)[[aa]]

(4)[[aa]]

(5)[[aa] [aa]]

{x}

(1)abab

(2)abab

(1)[[abab]]

(2)[[abab ab]]

{x,}

(?:ab){2,}

(1)ababab

(2)ababababab

(3)abab abab

(1)[[ababab]]

(2)[[ababababab]]

(3)[[abab] [abab]]

{x,}

(ab){2,}

(1)ababab

(2)ababababab

(3)abab abab

(1)[[ababab ab]]

(2)[[ababababab ab]]

(3)[[abab ab] [abab ab]]

{x,y}

(?:ab){1,3}

(1)abababab

(2)ab abababab

(3)ab ab ab

(1)[[ababab] [ab]]

(2)[[ab] [ababab] [ab]]

(3)[[ab] [ab] [ab]]

*

a*bc

(1)abc

(2)bc

(3)bcabc

(4)abcabc

(5)abcbcbc

(1)[[abc]]

(2)[[bc]]

(3)[[bc] [abc]]

(4)[[abc] [abc]]

(5)[[abc] [bc] [bc]]

+

ab+c

(1)abc

(2)abbbc

(3)ababc

(4)ab

(5)aaabc

(6)ab c

(1)[[abc]]

(2)[[abbbc]]

(3)[[abc]]

(4)[]

(5)[[abc]]

(6)[]

+

(1)abc

(2)abbbc

(3)ababc

(4)ab abc

(5)aaabc

(6)abc ababc

(1)[[abc]]

(2)[]

(3)[[ababc]]

(4)[[abc]]

(5)[[abc]]

(6)[[abc] [ababc]]

?

(1)abc

(2)ac

(3)bc

(4)abbc

(5)acac

(6)a c

(1)[[abc]]

(2)[[ac]]

(3)[]

(4)[]

(6)[]

?

(1)ac

(2)a c

(1)[[ac]]

(2)[[a c]]

Explain more:

  • (?:ab){2}, (ab){2}: Do you notice the differences between the non-capturing group and the capturing group? (?:...) can group them together and it won't save the characters inside the (?:) as another value, while the () will save them as another value. Try to find the differences by yourself!

  • (?:ab)+c: Use non-capturing to specify the specific word(s) you want the "+" or "*" to match.

  • ab?c: Same as the "+", only affects one word before it.

  • [[ac] [ac]]: Focus more here. each "ac" will be regarded as a match.

  • a ?c: Will match for zero or one time, so ac and a c can match.

Implementation restriction: The counting forms x{n,m}, x{n,}, and x{n} reject forms that create a minimum or maximum repetition count above 1000. Unlimited repetitions are not subject to this restriction.

Grouping

Include a or not? Affect a or not?

Usage

Grouping

(re)

numbered capturing group (submatch)

(?:re)

non-capturing group

(?flags:re)

set flags during re; non-capturing

Character

Usage

Input

Matches

(...)

(a)b(bc)

abbc

(...)

a( )b c

(1)ab c

(2)a bc

(3)a b c

(1)[]

(2)[]

a(?:b)(c)

abc

[[abc c]]

Explain more:

  • [[abbc a bc]]: It only matches one time, so its length is 1. It captures the a and bc, so it will show them behind the abbc as a new value respectively. Use {{index (index $matches 0) 0}} for the abbc; {{index (index $matches 0) 1}} for the a; {{index (index $matches 0) 2}} for the bc. In conclusion, the length of the $matches depends on how many times it matches, and if you capture x object(s), you can index inside $matches until position x.

  • [[a b c ]]: Although it only matches one thing (the length of $matches is 1)---a b c, it will capture the . So use the {{index (index $matches 0) 0}} for the a b c, and {{index (index $matches 0) 1)}} for the .

  • (?:...): It seems that (?:...) is useless, but when you want it to match a group of characters, for example, matching ab two times, it will be very useful because it will not save the capture of ab as another value. More example and explanation below (the {x}) part).

Flags

Special effects, default false.

Usage

Flags

i

case-insensitive

m

multi-line mode: ^ and $ match begin/end line in addition to begin/end text

s

let . match \n

Character

Comparison

Input

Matches

i

(1)(?i:hI)

(2)(?:hi)

hi

(1)[[hi]]

(2)[]

m

(1)(?m:abc$)

(2)(?:abc$)

fjiabc

heiyabc

jojabc

(1)[[abc] [abc] [abc]]

(2)[[abc]]

s

(1)(?s:a.b)

(2)(?:a.b)

a

b

(1)[[a b]]

(2)[]

Flag syntax is xyz (set) or -xyz (clear) or xy-z (set xy, clear z).

Empty Strings

Take effect at?

Usage

Empty strings

^

at beginning of text or line (m=true)

$

at end of text (like \z not \Z) or line (m=true)

\A

at beginning of text

\b

at ASCII word boundary (\w on one side and \W, \A, or \z on the other)

\B

not at ASCII word boundary

\z

at end of text

Escape Sequences

Usage

Escape sequences

\n

newline (≡ \012)

\*

literal *, for any punctuation character *

\x7f

hex character code (exactly two digits)

\x{10FFFF}

hex character code

\Q...\E

literal text ... even if ... has punctuation

Character Class Elements

Usage

Character class elements

x

single character

A-Z

character range (inclusive, using ASCII order to order the range)

\d

Perl character class

[:foo:]

ASCII character class foo

\p{Foo}

Unicode character class Foo

\pF

Unicode character class F (one-letter name)

Named Character Classes As Character Class Elements

Usage

Named character classes as character class elements

[\d]

digits (≡ \d)

[^\d]

not digits (≡ \D)

[\D]

not digits (≡ \D)

[^\D]

not not digits (≡ \d)

[[:name:]]

named ASCII class inside character class (≡ [:name:])

[^[:name]]

named ASCII class inside negated character class (≡ [:^name:])

[\p{Name}]

named Unicode property inside character class (≡ \p{Name})

[^\p{Name}]

named Unicode property inside negated character class (≡ \P{Name})

Perl Character Classes

Usage

Perl character classes (all ASCII-only)

\d

digits (≡ [0-9])

\D

not digits (≡ [^0-9])

\s

whitespace (≡ [\n ])

\S

not whitespace (≡ [^\n ])

\w

word characters (≡ [0-9A-Za-z_])

\W

not word characters (≡ [^0-9A-Za-z_])

ASCII Character Classes

Usage

ASCII Character Classes

[[:alnum:]]

alphanumeric (≡ [0-9A-Za-z])

[[:alpha:]]

alphabetic (≡ [A-Za-z])

[[:ascii:]]

ASCII (≡ [\x00-\x7F])

[[:blank:]]

blank

[[:cntrl:]]

control (≡ [\x00-\x1F\x7F])

[[:digit:]]

digits (≡ [0-9])

[[:graph:]]

graphical (≡ [!-~] ≡ [A-Za-z0-9!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~])

[[:lower:]]

lower case (≡ [a-z])

[[:print:]]

printable (≡ [ -~] ≡ [ [:graph:]])

[[:punct:]]

punctuation (≡ [!-/:-@[-`{-~])

[[:space:]]

whitespace (≡ [\n ])

[[:upper:]]

upper case (≡ [A-Z])

[[:word:]]

word characters (≡ [0-9A-Za-z_])

[[:xdigit:]]

hex digit (≡ [0-9A-Fa-f])

Examples:

Command: Regex, |; Catch words: reFindAllSubmatches function and save words into $matches.

Character

Usage

Input

Matches

^

^abd

(1)abdgt

(2)abcd

(3)abd abd abd

(1)[[abd]]

(2)[]

(3) [[abd]]

$

abd$

(1)abd

(2)ddd

(3)abcabdacd

(1)[[abd]]

(2)[]

(3)[]

.

a...e..h

(1)aeh

(2)a...e..h

(3)a$%^e&*h

(4)abcdefghabc

(1)[]

(3)[[a$%^e&*h]]

(4)[[abcdefgh]]

[...]

[a.(b)+c ]

(1)a

(2)abcc a

(3).

(4)()+

(5)a b

(1)[[a]]

(2)[[a] [b] [c] [c] [a]]

(3)[[.]]

(4)[[(] [)] [+]]

[^...]

[^abc]

acd

[[d]]

[a-z]

(1)8aZ

(2)^ g

(1)[[8] [a] [Z]]

(2)[[^] [g]]

\$\&\(\)

$&()

[[$&()]]

Explain more:

  • [[a...e..h]]:. means matching any character.

  • [[a] [ ] [b]]: It can match , but you need to type something behind the , or it won't match.

  • \: Escape the specific characters mentioned above, so you can match those special characters. Also, "\" can create an escape sequence.

PreviousCustom EmbedsNextTexas Hold'em

Last updated 4 years ago

Was this helpful?

(4)

(2)

(3)

(5)

(1)

(2)

(5)

(3)

(2)

(5)

[0-z]: The characters are ordered by , so it will match ^ and all characters that are behind the 0 (or the 0 itself).

ASCII code
[[abc] [a]]
[[abc ]]
[[t t] [t t]]
[]
(?:ab){2}
(ab){2}
(?:ab)+c
ab?c
[[ac] [ac]]
a ?c
[[abbc a bc]]
[[a b c ]]
(?:...)
[[a...e..h]]
[[a] [ ] [b]]
[0-z]
\