Match () with regex () (): Difference between revisions

From PenguinMod Wiki
Jump to navigation Jump to search
Content added Content deleted
(finished the page)
(added flags)
Line 4: Line 4:


=== Use ===
=== Use ===
This block searches for every instance of the selected rule. The rule can be inserted into the second input box. The third input box acts as a ? (please add what this input box does)
This block searches for every instance of the selected rule. The rule can be inserted into the second input box. The third input box acts as a flag (more info later on)


=== Examples ===
=== Examples ===
Line 27: Line 27:
|-
|-
|.
|.
|This is the syntax for any character
|This is the syntax for (almost) any character (see flags)
|["gray grey griy gary"] ["gr'''.'''y"] ["g"] -> ["gray", "grey", "griy"]
|["gray grey griy gary"] ["gr'''.'''y"] ["g"] -> ["gray", "grey", "griy"]
|-
|-
Line 134: Line 134:
["I would like to find all \s in this very \-filled \text"] ["'''\\'''"] ["g"] -> ["\","\","\"]
["I would like to find all \s in this very \-filled \text"] ["'''\\'''"] ["g"] -> ["\","\","\"]
|}
|}

=== Flags ===
Flags are used to redefine the behaviour of a regular expression. These flags include:
{| class="wikitable"
|+Flags
!Flag
!Name
!Description
|-
|g
|Global
|Finds all the matching sub-strings not only the first one
|-
|i
|Case-insensitive
|Makes the rule case-insensitive. For example if the rule is [e] and the flag [ig] all e '''and''' E's get matched
|-
|m
|multi-line
|Changes the meaning of the rule $ and ^ to be the end and beginning of a line and whole string
|-
|s
|dotalls
|changes the . character to include '''every''' character including new-line
|}
To use multiple flags you write them, as seen in the description of the case-insensitive flag, after another with '''no delimiter''' (a space or comma inbetween the flags). Order does not matter.

More flags can be found [https://www.codeguage.com/courses/regexp/flags here]


== Comments ==
== Comments ==

Revision as of 18:04, 10 June 2024

Information

match with regex block
match with regex block

The match regex block is a reporter block that matches a string with a regular expression, a regex.

Use

This block searches for every instance of the selected rule. The rule can be inserted into the second input box. The third input box acts as a flag (more info later on)

Examples

Foo bar example

match with regex: first example
Example 1

In this example inputs of the block it searches for the string "foo" in the string "foo bar" which will result in the array ["foo"] since there is only one foo in the string.

Fruit example

match with regex: second example
Example 2

For this example the block searches for every occurance of the word "banana" which results in the array ["banana","banana","banana"].

Regular expression rule syntax

The rules of regular expressions have their own syntax which you can all look up on this website.

The examples will appear as: [to be matched string] [rule] [third input] -> [result]

Syntax Table
meta characters Description Example
. This is the syntax for (almost) any character (see flags) ["gray grey griy gary"] ["gr.y"] ["g"] -> ["gray", "grey", "griy"]
() groups a set of characters No actual change to the result, will be needed later on
+ matches the preceding character or group one or more times ["abbbbbba aba aa"] ["ab+a"] ["g"] -> ["abbbbbba","aba"]
? matches the preceding character or group zero or one times ["aba aa abba abbbba"] ["a?ba"] ["g"] -> ["aba","aa"]
* matches the preceding character or group any amount of times ["aba aa abbbbbbbba abbbbbbbbbbbbba"] ["ab*a"] ["g"] -> ["aba", "aa", "abbbbbbbba", "abbbbbbbbbbbbba"]
{M,N} matches the preceding character or group

a minimum of M to a naximum of N times

["aba abbba abbbbba abbbbbbbbbbba"] ["ab{3,5}a"] ["g"] -> ["abbba","abbbbba"]
{,N} matches the preceding character or group a maximum of N times ["aba abbba abbbbba abbbbbbbbbbba"] ["ab{,5}a"] ["g"] -> ["aba", "abbba", "abbbbba"]
{M,} matches the preceding character or group a minimum of M times ["aba abbba abbbbba abbbbbbbbbbba"] ["ab{3,}a"] ["g"] -> ["abbba", "abbbbba", "abbbbbbbbbbba"]
{N} matches the preceding character or group exactly N times ["aba abbba abbbbba abbbbbbbbbbba"] ["ab{3}a"] ["g"] -> ["abbba"]
[...] syntax for any characters or group within the rectangular parenthesis ["Hello Hallo Hmllo Hkllo"] ["H[ea]llo"] ["g"] -> ["Hello","Hallo"].

This can also used like this: ["Hello Hallo Hmllo Hkllo"] ["H[a-k]llo"] ["g] -> ["Hello","Hallo", "Hkllo"].

which acts as every letter in the alphabet from a to k

| seperating possible character or groups ["gray grey griy gary"] ["(gray|grey|griy)"] ["g"] -> ["gray", "grey", "griy"]
\w syntax for all Alphanumericals (a-z A-Z 0-9 and _) ["This is a_text90§!"] ["\w"] ["g"] -> ["T","h","i","s","i","s","a","_","t","e","x","t","9","0"]
\W syntax for all non-Alphanumericals not (a-z A-Z 0-9 and _) ["This is a_text90§!"] ["\W"] ["g"] -> ["§","!"]
\s syntax for the Whitespace which includes:

Ascii: tab, space, line feed, form feed and carriage return Unicode: matches, no-break spaces, next line, and the variable-width spaces (amongst others). Basically anything that is not visible

["This is a testing example"] ["\s"] ["g"] -> [" "," "," "]
\S syntax for all non-Whitespaces ["This is a tesing example"] ["\S"] ["g"] -> ["T","h","i","s","i","s","a","t","e","s","t","i","n","g","e","x","a"m","p","l","e"]
\d syntax for all Digits (0-9) ["My favourite number is 917"] ["\d"] ["g"] -> ["9","1","7"]
\D syntax for all non-Digits not (0-9) ["9 + 10 = 21"] ["\D"] ["g"] -> ["+","="]
^ syntax for the beginning of a string or line ["Hello World, I say Hello"] ["^Hello"] ["g"] -> ["Hello"]
$ syntax for the end of a string or line ["Hello World, I say Hello"] ["Hello$"] ["g"] -> ["Hello"]
\A syntax for the beginning of a string but not line ["Hello World,

Hello"] ["\A"] ["g"] -> ["Hello"]

\z syntax for the end of a string but not line ["Hello World,

Hello"] ["\z"] ["g"] -> ["Hello"]

[^...] syntax for any character or group not inside of the rectangular parenthesis ["Hello Hallo Hmllo Hkllo"] ["H[^ea]llo"] ["g"] -> ["Hmllo","Hkllo"].

This can also used like this: ["Hello Hallo Hmllo Hkllo"] ["H[^a-k]llo"] ["g] -> ["Hmllo"].

which acts as every letter but those in the alphabet from a to k

\ syntax for Escaping Characters.

For example used to get the actual character * instead of the syntax

["I would like to find all *s in this very *-filled *text"] ["\*"] ["g"] -> ["*","*","*"]

This is also used to escape itself: ["I would like to find all \s in this very \-filled \text"] ["\\"] ["g"] -> ["\","\","\"]

Flags

Flags are used to redefine the behaviour of a regular expression. These flags include:

Flags
Flag Name Description
g Global Finds all the matching sub-strings not only the first one
i Case-insensitive Makes the rule case-insensitive. For example if the rule is [e] and the flag [ig] all e and E's get matched
m multi-line Changes the meaning of the rule $ and ^ to be the end and beginning of a line and whole string
s dotalls changes the . character to include every character including new-line

To use multiple flags you write them, as seen in the description of the case-insensitive flag, after another with no delimiter (a space or comma inbetween the flags). Order does not matter.

More flags can be found here

Comments

if someone knows what the third input is for please add it here
-- Ninja 6734 (talk) 18:25, 8 June 2024 (UTC)