Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- Appetizer 08 - Text Processing
- -- Bruce Elliott
- -- May 2013
- function setup()
- -- String Library Tutorial at
- -- lua-users.org/wiki/StringLibraryTutorial
- -- Patterns Tutorial at
- -- lua-users.org/wiki/PatternsTutorial
- -- were used in this Appetizer. Some code was used or modified
- -- There are a large number of examples of how individual string
- -- statements can be used. Why you would use string proceesing is up to you.
- -- String format specifiers
- -- '%a' is all letters
- -- '%d' is all digits
- -- '%l' is all lower case characters
- -- '%u' is all upper case characters
- -- '%s' is a space character
- -- '[aeiou]' match any character in set
- -- '[^aeiou]' match all but characters in set
- -- 'w(...)' match w followed by any three characters - Captures
- -- 'an*' match longest zero or more repetitions of pattern
- -- 'an+' match one or more repetitions of pattern
- -- 'an-' match shortest zero or more repetitions of pattern
- -- 'an?' match zero or one occurrences of pattern
- -- Captures can be marked using parentheses: "q(...)"
- myString = "The quick brown fox jumped over the lazy dog. The fox jumped back."
- myString2 = "There are 15 apples out of 38 fruits and 76 tomatos"
- sTab =""
- -- string.find(s, pattern, init, plain)
- -- Looks for the first match of pattern in the string s.
- -- s - is string to search
- -- pattern - Find first match of pattern
- -- init - starting point for search
- -- plain - find substring no "magic" characters
- -- returns - start, end indices of match plus captures if any
- -- - returns nil if no match
- print("string.find()")
- strt, stop, sTab = string.find(myString, "%a")
- disTab(strt, stop, sTab)
- --> 1 1 First letter is at position 1
- strt, stop, sTab = string.find(myString, "%d")
- disTab(strt, stop, sTab)
- --> nil There are no digits
- strt, stop, sTab = string.find(myString2, "%d")
- disTab(strt, stop, sTab)
- --> 11 11 The first ddigit is at position 11
- strt, stop, sTab = string.find(myString, "%l")
- disTab(strt, stop, sTab)
- --> 2 2 First lower case char
- strt, stop, sTab = string.find(myString, "%u")
- disTab(strt, stop, sTab)
- --> 1 1 First upper case char
- strt, stop, sTab = string.find(myString, "%s")
- disTab(strt, stop, sTab)
- --> 4 4 first space char
- strt, stop, sTab = string.find(myString, "[wpt]")
- disTab(strt, stop, sTab)
- --> 14 14 First char in w p t (brown)
- strt, stop, sTab = string.find(myString, "[^wpt]")
- disTab(strt, stop, sTab)
- --> 1 1 First char not in wpt (T is not t)
- strt, stop, sTab = string.find(myString, "o(.)")
- disTab(strt, stop, sTab)
- --> 13 14 w First match of 'o' followed by any one char (brown)
- strt, stop, sTab = myString:find("o(.)")
- disTab(strt, stop, sTab)
- --> 13 14 w myString:find() instead of string.find(myString, )
- strt, stop, sTab = string.find(myString2, "pl*")
- disTab(strt, stop, sTab)
- --> 15 15 First p in apple
- strt, stop, sTab = string.find(myString2, "ck*")
- disTab(strt, stop, sTab)
- --> nil ck does not appear in string
- strt, stop = string.find("bananas", 'az*')
- disTab(strt, stop, sTab)
- --> 2 2
- strt, stop = string.find("bananas", 'an*')
- disTab(strt, stop)
- --> 2 3
- strt, stop = string.find("bannnnnanas", 'an*')
- disTab(strt, stop)
- --> 2 7 Notice ending point of 7
- strt, stop, sTab = string.find(myString2, "pl+")
- disTab(strt, stop, sTab)
- --> 16 17 First pl in apple
- strt, stop = string.find("bananas", 'an+')
- disTab(strt, stop)
- --> 2 3 Same result as 'an*'
- strt, stop = string.find("bannnnnanas", 'an+')
- disTab(strt, stop)
- --> 2 7 Same resukt as 'an*'
- strt, stop, sTab = string.find(myString2, "pl-")
- disTab(strt, stop, sTab)
- --> 15 15 First p in apple
- strt, stop = string.find("bananas", 'an-')
- disTab(strt, stop)
- --> 2 2 Compare to 'an*'
- strt = string.find("bannnnnanas", 'an-')
- disTab(strt, stop)
- --> 2 2 Compare to 'an*'
- strt, stop, sTab = string.find(myString2, "pl?")
- disTab(strt, stop, sTab)
- --> 15 15 First p in apple
- strt, stop, sTab = string.find(myString, "ox", 20)
- disTab(strt, stop, sTab)
- --> 52 53 First match starting at position 20 from start
- strt, stop, sTab = string.find(myString, "ox", -20)
- disTab(strt, stop, sTab)
- --> 52 53 First match starting 20 characters from the end
- -- string.format(formatstring, ..., target)
- -- Returns a formatted version of its variable arguments
- -- following the description given in the first argument,
- -- which must be a string
- -- formatstring - % and one or more of the following
- -- target - value(s) to be formatted
- -- Returns: A formatted string
- -- Format specifiers must be preceded by a "%"
- -- c, d, E, e, f, g, i, o, u, X, and x all expect a number
- -- q and s expect a string
- print("string.format()")
- print(string.format("%c %d %E %e", 65, 2, 3, 4))
- -- prints char w/val 65, int, EXP, exp
- print(string.format("%f %g", 5.67, 6))
- -- prints float, gen (int)
- print(string.format("%i %o %u %u %X %x", 5.5, 15, -2, 2, 11, 11))
- -- prints whole num, octal, unsigned int, unsig int, HEX (upper), hex (lower)
- num = 56
- newStr = string.format("%s %d", "The number is", num)
- print(newStr)
- --> The number is 56
- num = 56
- newStr = string.format("The number is %d", num)
- print(newStr)
- --> The number is 56
- ch = 'r'
- newStr = string.format("%s %s", "The char is", ch)
- print(newStr)
- --> The char is r "%s" prints a string
- ch = 'r'
- newStr = string.format("%s %q", "The char is", ch)
- print(newStr)
- --> The char is "r" "%q" puts quotes around string
- -- string.len(s)
- -- Receives a string and returns its length
- -- s - String to find length
- -- Returns: Length of the string s
- print("string.len()")
- strt = string.len(myString)
- print(strt)
- --> 66
- strt = myString:len()
- print(strt)
- --> 66 Note format
- -- string.lower(s)
- -- Receives a string and returns a copy of this string
- -- with all uppercase letters changed to lowercase.
- -- s - String to convert to all lower case
- -- Returns - Length of string s
- print("string.lower()")
- strt = string.lower(myString)
- print(strt)
- --> the quick brown fox jumped over the lazy dog. the fox jumped back.
- strt = myString:lower()
- print(strt)
- --> the quick brown fox jumped over the lazy dog. the fox jumped back.
- -- string.upper(s)
- --Recieves a string and returns a copy of this string
- -- with all lowercase letters changes to uppercase.
- -- s - String to convert to all upper case
- -- Returns - Uppercase version of string s
- print("string.upper()")
- strt = string.upper(myString)
- print(strt)
- --> THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. THE FOX JUMPED BACK
- strt = myString:upper()
- print(strt)
- --> THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. THE FOX JUMPED BACK
- -- string.match(s, pattern, init)
- -- Looks for the first match of pattern in the string s.
- -- s - String to search
- -- pattern - pattern to match
- -- init - starting location in the string
- -- returns - Captures from the first match of pattern
- -- in string s, or nil if none were found.
- -- Note: position information is not available
- print("string.match()")
- strt = string.match(myString, "the")
- print(strt)
- --> the
- strt = string.match(myString, "t(..)")
- print(strt)
- --> he
- strt = string.match("I have 2 questions for you", "%a %a")
- print(strt)
- --> I h Match a char followd by a char
- strt = string.match("I have 2 questions for you", "%a+ %a+")
- print(strt)
- --> I have Match any number of chars followed by any number of chars
- strt = string.match("I have 2 questions for you", "%d+ %a+")
- print(strt)
- --> 2 questions Match one or more digits followed by one or more chars
- strt = myString:match("b(....)")
- print(strt)
- --> rown
- -- string.rep(s, n)
- -- Returns a string that is the concatenation of n copies
- -- of the string s.
- -- s - String to replicate
- -- n - int, number of times to replicate the string
- -- Returns - n concatenations of string s
- print("string.rep()")
- strt = string.rep("abc", 5)
- print(strt)
- --> abcabcabcabcabc
- str = "abc"
- strt = str:rep(5)
- print(strt)
- --> abcabcabcabcabc
- -- string.sub(s, i, j)
- -- Returns the substring of s that starts at i
- -- and continues until j; i and j can be negative.
- -- s - find substring of this string
- -- i - int, starting index
- -- j - int, ending index
- -- Returns - Substring of string s
- print("string.sub()")
- strt = string.sub(myString, 5, 8)
- print(strt)
- --> quic
- strt = myString:sub(5, 9)
- print(strt)
- --> quick
- -- string.gsub(s, pattern, replace, n)
- -- It can replace all instances of the pattern
- -- provided with the replace string.
- -- s - Original string
- -- pattern - The string to replace
- -- replace - New string
- -- n - Used to limit the number of substitutions made
- -- Returns - the modified string and the
- -- number of substitutions
- print("string.gsub()")
- strt, stop = string.gsub(myString, "fox", "mouse", 1)
- print(strt)
- print(stop)
- --> The quick brown mouse jumped over the lazy dog. The fox jumped back.
- --> 1
- str = "123123123"
- strt, stop = str:gsub("2", "8")
- print(strt)
- print(stop)
- --> 183183183
- --> 3
- str = "The quick {brown} fox jumped {over} the {lazy dog}."
- strt, stop = string.gsub(str,"{(.-)}", function(a) print(a) end)
- print(stop)
- --> brown
- --> over
- --> lazy dog
- --> 3
- -- Note: This example used an anonymous function as the third parameter
- -- the function accepted each capture and printed it
- str = "The quick {brown} fox jumped {over} the {lazy dog}."
- strt, stop = string.gsub(str,"{(.-)}", myFunc)
- print(strt)
- print(stop)
- --> brown
- --> over
- --> lazy dog
- --> The quick rep fox jumped rep the rep.
- --> 3
- -- Note: Each capture is displayed by the function myFunc()
- -- myFunc returns "rep" which is substituted for
- -- the capture in the original string
- -- I replaced the function with the table sTab,
- -- which seems to be permitted. I was not able to
- -- display any table elements
- -- string.gmatch(s, pat)
- -- This returns a pattern finding iterator. The iterator will search through
- -- the string passed looking for instances of the pattern you passed.
- -- s - The string to search
- -- pat - the pattern to match
- -- Returns iterator
- print("string.gmatch()")
- for word in string.gmatch("Hello Codea user", "%a+") do
- print(word)
- end
- -- Note: the use of gmatch in a for loop
- strt = string.gmatch("Hello Codea user", "%a+")
- for word in strt do
- print(word)
- end
- --> Hello
- --> Codea
- --> user
- -- Note: strt is a function returned by string.gmatch()
- -- Change "%a+" to "%a" and see how the output changes
- --
- -- string.reverse(s)
- -- Reverses a string
- -- s - String to reverse
- -- Returns - string reversed
- print("string.reverse()")
- strt = string.reverse("Hello Codea user")
- print(strt)
- --> resu aedoC olleH
- str = "This is an example"
- strt = str:reverse()
- print(strt)
- --> elpmaxe na si sihT
- end
- function disTab(beg, lst, nTab)
- if beg ~= nil and lst ~= nil then
- print(beg.." "..lst)
- else
- print("nil")
- end
- if nTab ~= nil then
- print(nTab)
- end
- end
- function myFunc(value)
- print(value)
- return("rep")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement