User:Legend26/Test

From Legacy Roblox Wiki
Revision as of 17:42, 10 July 2011 by >Legend26
Jump to navigationJump to search
  • Work in progress. When it's done I'll release it.
  • If you find something wrong, have some edit to make, or a way to make it easier to read, go ahead and edit this. It's a wiki after all.
  • Article will be named "Beginner's Guide to String Patterns" when finished or something similar.
  • TO DO: Rearrange example list after finishing the parts telling what everything in a string pattern does so the examples actually make sense

What are String Patterns?

String patterns are, in essence, just strings. What makes them different from ordinary strings then, you ask? String patterns are strings that use a special combination of characters. These characters combinations are generally used with functions in the string library such as 'string.match' and 'string.gsub' to do interesting things with strings. For instance, with string patterns you can do something like this:

local s = "I am a string!"
for i in string.gmatch(s, "[^%s]+") do --Where "[^%s]+" is the string pattern.
    print(i)
end

Output:
I
am
a
string!

But what makes the code above so cool? Perhaps you've wanted to make a list of people without using a table, or maybe you need to parse a string. String patterns can help do this!


The Basics of String Patterns

As said before, string patterns are strings that look a little different and are used for a different purpose than what strings are usually used for. Here we will look at the basics of just what make a string pattern up. Here we will look at just what the different parts of a string pattern mean.

Character Classes

Character classes in string patterns stand for a range or set of characters. Let's look at the classes listed below.

  • %a
  • This character class represents all letters no matter if they're lowercase or uppercase.
  • Some examples are: 'a', 'd', 'F', and 'G'.
  • %l
  • This character class represents all lowercase letters.
  • Some examples are: 'a', 'd', 'f', and 'g'.
  • %u
  • This character class represents all uppercase letters.
  • Some examples are: 'A', 'B', 'D', and 'Z'.
  • %p
  • This character class represents all punctuation characters.
  • Some examples are: ".", "?", "+", and "/".
  • %w
  • This character class represents all alphanumeric letters.
  • This means that this class encompasses both letters and numbers.
  • Some examples are 'A', 'f', '3', and '7'.
  • %s
  • This character class represents all space characters.
  • Some examples are ' ', '\n', and '\r'
  • %c
  • This character class represents all control characters.
  • Control characters are characters with an ASCII code below 32 and also ASCII code 127
  • Control characters are all non-printing meaning that they don't represent a symbol representation.
  • %x
  • This character class represents all hexadecimal (Base 16) characters.
  • Some examples are '21' which represents '!' and '5A' which represents 'Z'
  • %z
  • This character class represents the character '\0'.
  • This character is commonly referred to as NUL.
  • The dot character class
  • This class is represented by a single dot '.'
  • This class represents all characters, every single one.
  • Unlike the others, it is not preceded by a '%' sign.


As you can see, each of the character classes are used to represent a set of characters. Now let's look at some of the many things we can do with just these character classes.

One of the things you might notice about the above, is that they are all lowercase. Making them capitals reverses their effect. For instance, %s represents spaces, but %S represents everything except space characters. %l represents lowercase letters which %L represents its compliment, all characters except those that are lowercase letters.

  • Add examples
  • Talk about things like having a + and - and such after the class
  • Have examples show you to use stuff like %p%u+ so they don't get confused when they try out sets


Pattern Items

  • To do


Sets

Sets are used when using only a character class is not enough. For instance, you might want to match both lowercase letters (%l) as well as punctuation characters (%p) using a single class. So how would we do this? Let's take a look at this example:

local s = "123 Hello! I am another string."
local Pattern = "[%l%p]+"
print(string.match(s, Pattern))

Output:
>ello!

As you can see from the example, sets are defined by the '[' and ']' around them. You also see that the classes for lowercase letters and punctuation is contained within. This means that the set will act as a class that represents both lowercase and punctuation, unlike if you used %l%p which would match the sequence of a punctuation character following a lowercase letter.


You aren't restricted to using only character classes, though! You can also use normal characters to add to the set. Also, you can specify a range of characters with the '-' symbol. Let's see how this works in the following example:

local NormCharP = "[3_%l]+" --A set representing lowercase letters, a three, and an underscore that matches 0 or more repetitions.
local RangeP = "[1-4%u]+" --A set representing the numbers 1 to 4 as well as uppercase letters that matches 0 or more repetitions.
local s1 = "Random_123"
local s2 = "37913 Sandwiches!"

for i in string.gmatch(s1, NormCharP) do
    print(i)
end
print("--Next--")
for i in string.gmatch(s2, RangeP) do
    print(i)
end

Output:
andom_
3
--Next--
3
13
S


From the example, you can see how string.gmatch manipulated strings s1 and s2 using the string patterns. And yet, there's still one last thing you can do. Now you can see just how the example in the introduction works. Let's take a look at this code:

local Pattern = "[^%s1-9]+" 
local s = "He29ll0, I like strings1"
local temp = "
for i in string.gmatch(s, Pattern) do
    temp = temp .. i
end
print(temp)

Output:
Hell0,Ilikestrings

This pattern is the compliment of [%s1-9] meaning that it will represent all characters except the space characters and the numbers 1 to 9. This is defined by using the '^' character at the beginning of the set. All it does is makes the set act the direct opposite of a normal set. As you can easily see from this example, the spaces and number 29 in the middle of 'Hello' were removed.

Captures

  • To do

Examples

  • To do

A common way of using these string patterns are using them with string functions. Here's an example:

Example
theTime = "12:47"
for i in string.gmatch(theTime, "%p") do 
   print(p)
end

Output:
>":" --as you can tell, the ":" (colon) character is the only punctuation character there is in the string!


Also, you can captures to find string information.

Example
twentyOne = "Twenty one = 21"

_, _, a, b = string.find(twentyOne, "(%a+%s%a+)%s=%s(%d+)") --see how I used parenthesis to designate my captures? "a" is the first capture, and "b" is the second capture.

print(a, b)

Output:
>"Twenty one 21" --See how it only printed the captures designated by the parenthesis?


See also