Patterns: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
No edit summary
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">"
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
===Patterns===
===Patterns===
{{CatUp|Function Dump/String Manipulation}}
{{EmphasisBox|[[Image:RsClose_ds.png]]  '''Note:''' This tutorial requires some knowledge of [[Function_Dump/String_Manipulation|string manipulation]].|red|}}
{{EmphasisBox|[[Image:RsClose_ds.png]]  '''Note:''' This tutorial requires some knowledge of [[Function_Dump/String_Manipulation|string manipulation]].|red|}}
<br/>
 
Patterns are very useful tools for string manipulation. They allow you to do operations on all numbers in a string,  
Patterns are very useful tools for string manipulation. They allow you to do operations on all numbers in a string,
 
==Classes==
==Classes==
Character Class:
Character Class:
Line 11: Line 11:
*'''.'''  — Represents all characters (#32kas321fslk#?@34)
*'''.'''  — Represents all characters (#32kas321fslk#?@34)
*'''%a''' — Represents all letters (aBcDeFgHiJkLmNoPqRsTuVwXyZ)
*'''%a''' — Represents all letters (aBcDeFgHiJkLmNoPqRsTuVwXyZ)
*'''%bxy''' — See [[Patterns#Pattern_Items|Pattern Items]] below
*'''%c''' — Represents all control characters (all [http://wiki.roblox.com/index.php/Image:Ascii_Table.png ascii characters] below 32 and ascii character 127)
*'''%c''' — Represents all control characters (all [http://wiki.roblox.com/index.php/Image:Ascii_Table.png ascii characters] below 32 and ascii character 127)
*'''%d''' — Represents all base-10 digits (1-10)
*'''%d''' — Represents all base-10 digits (0-9)
*'''%l''' — Represents all lower-case letters (abcdefghijklmnopqrstuvwxyz)
*'''%l''' — Represents all lower-case letters (abcdefghijklmnopqrstuvwxyz)
*'''%p''' — Represents all punctuation characters (#^;,.) etc.
*'''%p''' — Represents all punctuation characters (#^;,.) etc.
Line 20: Line 21:
*'''%x''' — Represents all hexadecimal digits (0123456789ABCDEF)
*'''%x''' — Represents all hexadecimal digits (0123456789ABCDEF)
*'''%z''' — Represents the [http://wiki.roblox.com/index.php/Image:Ascii_Table.png ascii character] with representation 0 (the null terminator)
*'''%z''' — Represents the [http://wiki.roblox.com/index.php/Image:Ascii_Table.png ascii character] with representation 0 (the null terminator)
*'''%x''' — Represents (where x is ''any non-alphanumeric character'') the character x. This is the standard way to escape the magic characters. Any punctuation character (even the non magic) can be preceded by a '%' when used to represent itself in a pattern. So, a percent sign in a string is "%%"  <br/>
*'''%x''' — Represents (where x is ''any '''non-alphanumeric''' character'') the character x. This is the standard way to escape the magic characters. Any punctuation character (even the non magic) can be preceded by a '%' when used to represent itself in a pattern. So, a percent sign in a string is "%%"  <br/>
Here's an example:
Here are some examples:


{{Example|{{Code and output|code=
{{Code and output|fit=output|code=
String="Ha! You'll never find any of these (323414123114452) numbers inside me!"
String="Ha! You'll never find any of these (323414123114452) numbers inside me!"
print(string.match(String, "%p")) -- Find a punctuation character
print(string.match(String, "%p")) -- Find a punctuation character
|output=
|output=
!
!
}}}}
}}


An upper-case version of any of these classes results in the complement of that class. For instance, %A will represent all
An upper-case version of any of these classes results in the complement of that class. For instance, %A will represent all
non-letter characters. Cool thing? You can combine character classes! Here's another example:
non-letter characters. Cool thing? You can combine character classes! Here's another example:
{{Example|{{Code and output|code=
{{Code and output|fit=output|code=
Martian="141341432431413415072343E334141241312"
Martian="141341432431413415072343E334141241312"
print(Martian:match("%D%d")) -- Find any non-digit character immediately followed by a digit.
print(Martian:match("%D%d")) -- Find any non-digit character immediately followed by a digit.
|output=
|output=
E3
E3
}}}}
}}


==Modifiers==
==Modifiers==
In Lua, modifiers are used for repetitions and optional parts. That's where they're useful; you can get more than one character at a time:
In Lua, modifiers come '''after''' character classes and are used for repetitions and optional parts, which is why they're useful. Here is a list:


* + — 1 or more repetitions
* + — 1 or more repetitions
Line 46: Line 47:
* - — (minus sign) also 0 or more repetitions
* - — (minus sign) also 0 or more repetitions
* ? — optional (0 or 1 occurrence)
* ? — optional (0 or 1 occurrence)
<br/>
 
 
I'll start with the simplest one: the ?. This makes the character class optional, and if it's there, captures 1 of it. That sounds complex, but is actually really simple, so here's an example:
I'll start with the simplest one: the ?. This makes the character class optional, and if it's there, captures 1 of it. That sounds complex, but is actually really simple, so here's an example:
{{Example|{{Code and output|code=
{{Example|{{Code and output|fit=output|code=
stringToMatch="Once upon a time, in a land far, far away..."
stringToMatch="Once upon a time, in a land far, far away..."
print(stringToMatch:match("%a?")) -- Find a letter, but it doesn't have to be there.
print(stringToMatch:match("%a?")) -- Find a letter, but it doesn't have to be there.
print(stringToMatch:match("%d?")) -- Find a number, but it doesn't have to be there.
print(stringToMatch:match("%d?")) -- Find a number, but it doesn't have to be there.
|output=
|output=
O -- O, in Once.
O
--Nothing because the digit didn't need to be there, so nothing was returned.
}}
}}}}
The '''O''' is the '''O''' in '''Once'''. <br/>
<br/>
Nothing is returned the second time because the digit didn't need to be there.
The + symbol used after a character class requires at least one instance of that class, and will get the longest string of that class. Here's an example:
}}
{{Example|{{Code and output|code=
 
 
The + symbol requires at least one instance of that class, and will get the longest string of that class. Here's an example:
{{Example|{{Code and output|fit=output|code=
stringToMatch="Once upon a time, in a land far, far away..."
stringToMatch="Once upon a time, in a land far, far away..."
print(stringToMatch:match("%a+")) -- Finds the first letter, then matches letters until a non-letter character
print(stringToMatch:match("%a+")) -- Finds the first letter, then matches letters until a non-letter character
Line 64: Line 69:
|output=
|output=
Once
Once
nil -- Nil because the pattern required the digit to be there, but it wasn't, so nil is returned.
nil
}}}}
}}
'''Once''' is the first set of only letters—the space between '''once''' and '''upon''' is not a letter, so the pattern stops there. <br/>
The second time '''nil''' was returned because the pattern required the digit to be there, but it wasn't.
}}


<br/>
 
The * symbol used after a character class is like a combination of the + and ? modifiers. It matches the longest sequence of the character class, but it doesn't have to be there. Here's an example of it matching a floating-point (decimal) number, without requiring the decimal:
The * symbol is like a combination of the + and ? modifiers. It matches the longest sequence of the character class, but it doesn't have to be there. Here's an example of it matching a floating-point (decimal) number, without requiring the decimal:
{{Example|{{Code and output|code=
{{Example|{{Code and output|fit=output|code=
numPattern="%d+%.?%d*"
numPattern="%d+%.?%d*"
--[[ Requires there to be an integer, and if there's a decimal point, get it (remember: a period is magic character, so you have to escape it with the % sign), and if there are numbers after the decimal point, grab them. ]]
--[[ Requires there to be an integer, if there's a decimal point, get it (remember: a period is magic character, so you have to escape it with the % sign), and if there are numbers after the decimal point, grab them. ]]


local num1="21608347 is an integer, a whole number, and a natural number!"
local num1="21608347 is an integer, a whole number, and a natural number!"
Line 78: Line 86:
print(num2:match(numPattern))
print(num2:match(numPattern))
|output=
|output=
Output:
21608347
21608347 -- Grabbed the whole number, but there wasn't a decimal point nor were there numbers after the decimal point
2034782.014873
2034782.014873 -- Grabbed the floating-point number, because it had a decimal and numbers after it
}}
}}}}
The first result was the whole number, but there wasn't a decimal point nor were there numbers after the decimal point <br/>
<br/>
The second result was the floating-point number, because it had a decimal and numbers after it
The - symbol used after a character class is like the * symbol; actually, there's only one difference: It matches the shortest sequence of the character class. Here's an example showing the difference:
}}
{{Example|{{Code and output|code=
 
 
The - symbol is like the * symbol; actually, there's only one difference: It matches the shortest sequence of the character class. Here's an example showing the difference:
{{Example|{{Code and output|fit=output|code=
String="((3+4)+3+4)+2"
String="((3+4)+3+4)+2"
print(String:match("%(.*%)")) -- Find a (, then match all (the . represens all characters) characters until the LAST ).
print(String:match("%(.*%)")) -- Find a (, then match all (the . represents all characters) characters until the LAST ).
print(String:match("%(.-%)")) -- Find a (, then match all characters until the FIRST ).
print(String:match("%(.-%)")) -- Find a (, then match all characters until the FIRST ).
|output=
|output=
((3+4)+3+4) -- Grabbed everything from the first opening parenthesis to the last closing parenthesis
((3+4)+3+4)
((3+4)      -- Grabbed everything from the first opening parenthesis to the first closing parenthesis
((3+4)
}}}}
}}
The first result was everything from the first opening parenthesis to the '''last''' closing parenthesis <br/>
The second result everything from the first opening parenthesis to the '''first''' closing parenthesis
}}


==Sets==
==Sets==
Line 97: Line 111:


The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.
The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.
* '''[^set]''' represents the complement of set, where set is interpreted as above.
* '''[^set]''' represents the complement of set. The complement of a set is everything not in that set. [^set] is to [set] as %A is to %a.


{{Example|{{Code and output|code=
{{Example|{{Code and output|fit=output|code=
Vowel="[AEIOUaeiou]" -- Match a vowel, upper-case or lower-case
Vowel="[AEIOUaeiou]" -- Match a vowel, upper-case or lower-case
Nonvowel="[^AEIOUaeiou]" -- Match any non-vowel by using the complement of the vowel set
Nonvowel="[^AEIOUaeiou]" -- Match any non-vowel by using the complement of the vowel set
Line 109: Line 123:


|output=
|output=
I-- First vowel
I
-- This is a space; it was the first non-vowel character (after the I).
0-- First octal digit, late in the string.
0
}}}}
}}
The first result is the first vowel <br/>
The second result is the first non-vowel character (a space, which was right after the I) <br/>
The 0 was the first octal digit, late in the string.
}}


==Pattern Items==
==Pattern items==


Alright, now it's time to explain what a pattern item is. A pattern item may be:
Alright, now it's time to explain what a pattern item is. A pattern item may be:
Line 126: Line 144:
* %bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b() matches expressions with balanced parentheses.
* %bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b() matches expressions with balanced parentheses.


{{Example|{{Code and output|code=
{{Example|{{Code and output|fit=output|code=
local Code="((I'm hiding inside parentheses!) I'll never) be found!"
local Code="((I'm hiding inside parentheses!) I'll never) be found!"
print(Code:match("%b()"))
print(Code:match("%b()"))
|output=
|output=
((I'm hiding inside parentheses!) I'll never) -- First opening parenthesis out of 2, second closing parenthesis out of 3
((I'm hiding inside parentheses!) I'll never)
}}}}
}}
The pattern found two opening parentheses, so it went to find two closing ones, trapping everything in between the first opening parenthesis and the second closing parenthesis.
The pattern found two opening parentheses, so it went to find two closing ones, trapping everything in between the first opening parenthesis and the second closing parenthesis.
}}


Pattern:
===Patterns===


A pattern is a sequence of pattern items. A '^' at the beginning of a pattern anchors the match at the beginning of the string. A '$' at the end of a pattern anchors the match at the end of the string. At other positions, '^' and '$' have no special meaning and represent themselves. Also, a pattern cannot contain embedded zeroes. Use %z instead. Here's an example of a pattern:
A pattern is a sequence of pattern items. A '^' at the beginning of a pattern anchors the match at the beginning of the string. A '$' at the end of a pattern anchors the match at the end of the string. At other positions, '^' and '$' have no special meaning and represent themselves. Also, a pattern cannot contain embedded zeroes. Use %z instead. Here's an example of a pattern:


{{Example|{{Code and output|code=
{{Example|{{Code and output|fit=output|code=
local Pattern="[%w%s%p]*" -- Get the longest sequence containing alpha-numeric characters, punctuation marks, and spaces.
local Pattern="[%w%s%p]*" -- Get the longest sequence containing alpha-numeric characters, punctuation marks, and spaces.
local Pattern2="^%a+" -- The string has to start with a sequence of letters.
local Pattern2="^%a+" -- The string has to start with a sequence of letters.
Line 145: Line 164:
print(x:match(Pattern2))
print(x:match(Pattern2))
|output=
|output=
Hello, my name is Merlin! -- The entire string contained only alpha-numeric characters, punctuation marks, and spaces!
Hello, my name is Merlin!
Hello -- Matched only the letters at the start of the string.
Hello
}}}}
}}
The first result was the entire string because the entire string contained '''only''' alpha-numeric characters, punctuation marks, and spaces!
 
The second result matched only the letters at the start of the string.
}}


==Captures==
==Captures==


A pattern may contain sub-patterns enclosed in parentheses; they describe captures. When a match of a capture succeeds, the substring that match captures are stored (captured) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern "(a*(.)%w(%s*))", the part of the string matching "a*(.)%w(%s*)" is stored as the first capture (and therefore has number 1); the character matching "." is captured with number 2, and the part matching "%s*" has number 3. Whaaaaat??? Here:
A pattern may contain sub-patterns enclosed in parentheses; they describe captures. When a match of a capture succeeds, the substring that match captures are stored (captured) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern "(a*(.)%w(%s*))", the part of the string matching "a*(.)%w(%s*)" is stored as the first capture (and therefore has number 1); the character matching "." is captured with number 2, and the part matching "%s*" has number 3. Whaaaaat??? Here:
{{Example|{{Code and output|code=
{{Example|{{Code and output|fit=output|code=
local number="55"
local number="55"
print(number:find("%d%d")) -- Find returns the location of the match, not the match itself
print(number:find("%d%d")) -- Find returns the location of the match, not the match itself
print(number:find("(%d%d)"))
print(number:find("(%d%d)"))
|output=
|output=
1 2          -- The first digit is at number:sub(1,1) and the second digit is at number:sub(2,2)
1 2           
1 2 55 -- The 55 is captured and returned.
1 2 55
}}}}
}}
The second string had the parentheses represent a capture of one digit immediately followed by another. So, what a capture does is return whatever the function returns, which, for string.find, are the locations, as well as the ''matched substring''. What's inside the parentheses is the substring that is being matched. So, the %d%d was the substring that was to be matched, and it was returned along with the 1 and the 2, the values the function returns, followed by the matched substring (55).
The first result is because the first digit is at number:sub(1,1) and the second digit is at number:sub(2,2).
 
The second string had the parentheses represent a capture of one digit immediately followed by another. So, what a capture does is return whatever the function returns, which, for string.find, are the locations, as well as the ''matched substring''. What's inside the parentheses is the substring that is being matched. So, the %d%d was the substring that was to be matched, and the return values are the 1 and the 2, which are the values the function returns, followed by the matched substring, which is 55.
}}


As a special case, the empty capture () captures the current string position (a number). For instance, if we apply the pattern "()aa()" on the string "flaaap", there will be two captures: 3 and 5.
As a special case, the empty capture () captures the current string position (a number). For instance, if we apply the pattern "()aa()" on the string "flaaap", there will be two captures: 3 and 5.
Line 167: Line 193:


{{Quiz|title=Quiz on Patterns|
{{Quiz|title=Quiz on Patterns|
#What would be returned if you matched this pattern (assuming it is not nil): <tt>%a%s%d%p</tt>?
#What would be returned if you matched this pattern (assuming it is not nil): {{`|%a%s%d%p}}?
#What would the following code return? <code lua>
#What would the following code return? <syntaxhighlight lang="lua">
local Test = "Some games are very fun and adventurous!!@#!12468134972@#@!$!!"
local Test = "Some games are very fun and adventurous!!@#!12468134972@#@!$!!"
print(Test:match("%p-%d+%p"))
print(Test:match("%p-%d+%p"))
</code>
</syntaxhighlight>
#How does the '''-''' modifier differ from the '''*''' modifier?
#How does the '''-''' modifier differ from the '''*''' modifier?
#What is the simplest way to match "merlin11188" (including the quotes)?
#What is the simplest way to match "merlin11188" (including the quotes)?
Line 178: Line 204:
#!!@#!12468134972@
#!!@#!12468134972@
#The '''-''' modifier differs from the '''*''' modifier because the minus matches the shortest repetition of a character class, and the asterisk matches the longest.
#The '''-''' modifier differs from the '''*''' modifier because the minus matches the shortest repetition of a character class, and the asterisk matches the longest.
#Trick question. There are multiple ways to do this, but the simplest is undoubtedly: <code lua>
#Trick question. There are multiple ways to do this, but the simplest is undoubtedly: <syntaxhighlight lang="lua">
local String = 'I have an account on ROBLOX. Signed, "merlin11188" (Definitely my signature)'
local String = 'I have an account on ROBLOX. Signed, "merlin11188" (Definitely my signature)'
String:match('"merlin11188"')
String:match('"merlin11188"')
</code>}}
</syntaxhighlight>}}




==See Also==
==See also==
*[http://www.lua.org/pil/20.2.html Programming in Lua: Patterns]
*[http://www.lua.org/pil/20.2.html Programming in Lua: Patterns]
*[http://www.lua.org/pil/20.3.html Programming in Lua: Captures]
*[http://www.lua.org/pil/20.3.html Programming in Lua: Captures]
*[http://www.lua.org/manual/5.1/manual.html#5.4.1 Lua 5.1 Reference Manual: Patterns]
*[http://www.lua.org/manual/5.1/manual.html#5.4.1 Lua 5.1 Reference Manual: Patterns]

Latest revision as of 06:19, 27 April 2023

Patterns

Note: This tutorial requires some knowledge of string manipulation.

Patterns are very useful tools for string manipulation. They allow you to do operations on all numbers in a string,

Classes

Character Class:

A character class is used to represent a set of characters. The following are character classes and their representations:

  • x — Where x is any non-magic character (^$()%.[]*+-?), x represents itself
  • . — Represents all characters (#32kas321fslk#?@34)
  • %a — Represents all letters (aBcDeFgHiJkLmNoPqRsTuVwXyZ)
  • %bxy — See Pattern Items below
  • %c — Represents all control characters (all ascii characters below 32 and ascii character 127)
  • %d — Represents all base-10 digits (0-9)
  • %l — Represents all lower-case letters (abcdefghijklmnopqrstuvwxyz)
  • %p — Represents all punctuation characters (#^;,.) etc.
  • %s — Represents all space characters
  • %u — Represents all upper-case letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
  • %w — Represents all alpha-numeric characters (aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789)
  • %x — Represents all hexadecimal digits (0123456789ABCDEF)
  • %z — Represents the ascii character with representation 0 (the null terminator)
  • %x — Represents (where x is any non-alphanumeric character) the character x. This is the standard way to escape the magic characters. Any punctuation character (even the non magic) can be preceded by a '%' when used to represent itself in a pattern. So, a percent sign in a string is "%%"

Here are some examples:

!
String="Ha! You'll never find any of these (323414123114452) numbers inside me!"
print(string.match(String, "%p")) -- Find a punctuation character

An upper-case version of any of these classes results in the complement of that class. For instance, %A will represent all non-letter characters. Cool thing? You can combine character classes! Here's another example:

E3
Martian="141341432431413415072343E334141241312"
print(Martian:match("%D%d")) -- Find any non-digit character immediately followed by a digit.

Modifiers

In Lua, modifiers come after character classes and are used for repetitions and optional parts, which is why they're useful. Here is a list:

  • + — 1 or more repetitions
  • * — 0 or more repetitions
  • - — (minus sign) also 0 or more repetitions
  • ? — optional (0 or 1 occurrence)


I'll start with the simplest one: the ?. This makes the character class optional, and if it's there, captures 1 of it. That sounds complex, but is actually really simple, so here's an example:

Example
O
stringToMatch="Once upon a time, in a land far, far away..."
print(stringToMatch:match("%a?")) -- Find a letter, but it doesn't have to be there.
print(stringToMatch:match("%d?")) -- Find a number, but it doesn't have to be there.

The O is the O in Once.
Nothing is returned the second time because the digit didn't need to be there.


The + symbol requires at least one instance of that class, and will get the longest string of that class. Here's an example:

Example

Once

nil
stringToMatch="Once upon a time, in a land far, far away..."
print(stringToMatch:match("%a+")) -- Finds the first letter, then matches letters until a non-letter character
print(stringToMatch:match("%d+")) -- Finds the first number, then matches numbers until a non-number character

Once is the first set of only letters—the space between once and upon is not a letter, so the pattern stops there.
The second time nil was returned because the pattern required the digit to be there, but it wasn't.


The * symbol is like a combination of the + and ? modifiers. It matches the longest sequence of the character class, but it doesn't have to be there. Here's an example of it matching a floating-point (decimal) number, without requiring the decimal:

Example

21608347

2034782.014873
numPattern="%d+%.?%d*"
--[[ Requires there to be an integer, if there's a decimal point, get it (remember: a period is magic character, so you have to escape it with the % sign), and if there are numbers after the decimal point, grab them. ]]

local num1="21608347 is an integer, a whole number, and a natural number!"
local num2="2034782.014873 is a decimal number!"
print(num1:match(numPattern))
print(num2:match(numPattern))

The first result was the whole number, but there wasn't a decimal point nor were there numbers after the decimal point
The second result was the floating-point number, because it had a decimal and numbers after it


The - symbol is like the * symbol; actually, there's only one difference: It matches the shortest sequence of the character class. Here's an example showing the difference:

Example

((3+4)+3+4)

((3+4)
String="((3+4)+3+4)+2"
print(String:match("%(.*%)")) -- Find a (, then match all (the . represents all characters) characters until the LAST ).
print(String:match("%(.-%)")) -- Find a (, then match all characters until the FIRST ).

The first result was everything from the first opening parenthesis to the last closing parenthesis
The second result everything from the first opening parenthesis to the first closing parenthesis


Sets

  • [set] represents the class which is the union of all characters in the set. You define a set with brackets, like [%a%d]. A range of characters may be specified by separating the end characters of the range with a '-'. All classes described above may also be used as components in set. All other characters in a set represent themselves. For example, [%w_] (or [_%w]) represents all alphanumeric characters plus the underscore, [0-7] represents the octal digits, and [0-7%l%-] represents the octal digits plus the lowercase letters plus the '-' character.

The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.

  • [^set] represents the complement of set. The complement of a set is everything not in that set. [^set] is to [set] as %A is to %a.
Example

I

0
Vowel="[AEIOUaeiou]" -- Match a vowel, upper-case or lower-case
Nonvowel="[^AEIOUaeiou]" -- Match any non-vowel by using the complement of the vowel set
OctalDigit="[0-7]" -- Match an octal digit. Octal digits: 0,1,2,3,4,5,6,7
stringToMatch="I have several vowels and consonants, and I'm followed by an octal number: 0231356701"
print(stringToMatch:match(Vowel))
print(stringToMatch:match(Nonvowel))
print(stringToMatch:match(OctalDigit))

The first result is the first vowel
The second result is the first non-vowel character (a space, which was right after the I)
The 0 was the first octal digit, late in the string.


Pattern items

Alright, now it's time to explain what a pattern item is. A pattern item may be:

  • a single character class, which matches any single character in the class;
  • a single character class followed by '*', which matches 0 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;
  • a single character class followed by '+', which matches 1 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;
  • a single character class followed by '-', which also matches 0 or more repetitions of characters in the class. Unlike '*', these repetition items will always match the shortest possible sequence;
  • a single character class followed by '?', which matches 0 or 1 occurrence of a character in the class;
  • %n, for n between 1 and 9; such item matches a substring equal to the n-th captured string (see Captures below);
  • %bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b() matches expressions with balanced parentheses.
Example
((I'm hiding inside parentheses!) I'll never)
local Code="((I'm hiding inside parentheses!) I'll never) be found!"
print(Code:match("%b()"))

The pattern found two opening parentheses, so it went to find two closing ones, trapping everything in between the first opening parenthesis and the second closing parenthesis.


Patterns

A pattern is a sequence of pattern items. A '^' at the beginning of a pattern anchors the match at the beginning of the string. A '$' at the end of a pattern anchors the match at the end of the string. At other positions, '^' and '$' have no special meaning and represent themselves. Also, a pattern cannot contain embedded zeroes. Use %z instead. Here's an example of a pattern:

Example

Hello, my name is Merlin!

Hello
local Pattern="[%w%s%p]*" -- Get the longest sequence containing alpha-numeric characters, punctuation marks, and spaces.
local Pattern2="^%a+" -- The string has to start with a sequence of letters.
x="Hello, my name is Merlin!"
print(x:match(Pattern))
print(x:match(Pattern2))

The first result was the entire string because the entire string contained only alpha-numeric characters, punctuation marks, and spaces!

The second result matched only the letters at the start of the string.


Captures

A pattern may contain sub-patterns enclosed in parentheses; they describe captures. When a match of a capture succeeds, the substring that match captures are stored (captured) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern "(a*(.)%w(%s*))", the part of the string matching "a*(.)%w(%s*)" is stored as the first capture (and therefore has number 1); the character matching "." is captured with number 2, and the part matching "%s*" has number 3. Whaaaaat??? Here:

Example

1 2

1 2 55
local number="55"
print(number:find("%d%d")) -- Find returns the location of the match, not the match itself
print(number:find("(%d%d)"))

The first result is because the first digit is at number:sub(1,1) and the second digit is at number:sub(2,2).

The second string had the parentheses represent a capture of one digit immediately followed by another. So, what a capture does is return whatever the function returns, which, for string.find, are the locations, as well as the matched substring. What's inside the parentheses is the substring that is being matched. So, the %d%d was the substring that was to be matched, and the return values are the 1 and the 2, which are the values the function returns, followed by the matched substring, which is 55.


As a special case, the empty capture () captures the current string position (a number). For instance, if we apply the pattern "()aa()" on the string "flaaap", there will be two captures: 3 and 5.

Quiz

Quiz on Patterns


  1. What would be returned if you matched this pattern (assuming it is not nil): %a%s%d%p?
  2. What would the following code return?
    local Test = "Some games are very fun and adventurous!!@#!12468134972@#@!$!!"
    print(Test:match("%p-%d+%p"))
    
  3. How does the - modifier differ from the * modifier?
  4. What is the simplest way to match "merlin11188" (including the quotes)?

Note: This is a quiz. The answers are not explained. If you don't understand, review this page, then ask in the forums

  1. You would get a letter, followed by a space, followed by a single-digit number, followed by a punctuation mark.
  2. !!@#!12468134972@
  3. The - modifier differs from the * modifier because the minus matches the shortest repetition of a character class, and the asterisk matches the longest.
  4. Trick question. There are multiple ways to do this, but the simplest is undoubtedly:
    local String = 'I have an account on ROBLOX. Signed, "merlin11188" (Definitely my signature)'
    String:match('"merlin11188"')
    



See also