Advertisement
br1wr2el3

App 08 - String Processing

May 5th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.13 KB | None | 0 0
  1. --# Main
  2. -- Appetizer 08 - Text Processing
  3.  
  4. -- Bruce Elliott
  5. -- May 2013
  6.  
  7. function setup()
  8.  
  9. -- String Library Tutorial at
  10. -- lua-users.org/wiki/StringLibraryTutorial
  11. -- Patterns Tutorial at
  12. -- lua-users.org/wiki/PatternsTutorial
  13. -- were used in this Appetizer. Some code was used or modified
  14.  
  15. -- There are a large number of examples of how individual string
  16. -- statements can be used. Why you would use string proceesing is up to you.
  17.  
  18. -- String format specifiers
  19. -- '%a' is all letters
  20. -- '%d' is all digits
  21. -- '%l' is all lower case characters
  22. -- '%u' is all upper case characters
  23. -- '%s' is a space character
  24. -- '[aeiou]' match any character in set
  25. -- '[^aeiou]' match all but characters in set
  26. -- 'w(...)' match w followed by any three characters - Captures
  27. -- 'an*' match longest zero or more repetitions of pattern
  28. -- 'an+' match one or more repetitions of pattern
  29. -- 'an-' match shortest zero or more repetitions of pattern
  30. -- 'an?' match zero or one occurrences of pattern
  31.  
  32. -- Captures can be marked using parentheses: "q(...)"
  33.  
  34. myString = "The quick brown fox jumped over the lazy dog. The fox jumped back."
  35. myString2 = "There are 15 apples out of 38 fruits and 76 tomatos"
  36. sTab =""
  37.  
  38. -- string.find(s, pattern, init, plain)
  39.  
  40. -- Looks for the first match of pattern in the string s.
  41.  
  42. -- s - is string to search
  43. -- pattern - Find first match of pattern
  44. -- init - starting point for search
  45. -- plain - find substring no "magic" characters
  46. -- returns - start, end indices of match plus captures if any
  47. -- - returns nil if no match
  48.  
  49. print("string.find()")
  50.  
  51. strt, stop, sTab = string.find(myString, "%a")
  52. disTab(strt, stop, sTab)
  53. --> 1 1 First letter is at position 1
  54.  
  55. strt, stop, sTab = string.find(myString, "%d")
  56. disTab(strt, stop, sTab)
  57. --> nil There are no digits
  58.  
  59. strt, stop, sTab = string.find(myString2, "%d")
  60. disTab(strt, stop, sTab)
  61. --> 11 11 The first ddigit is at position 11
  62.  
  63. strt, stop, sTab = string.find(myString, "%l")
  64. disTab(strt, stop, sTab)
  65. --> 2 2 First lower case char
  66.  
  67. strt, stop, sTab = string.find(myString, "%u")
  68. disTab(strt, stop, sTab)
  69. --> 1 1 First upper case char
  70.  
  71. strt, stop, sTab = string.find(myString, "%s")
  72. disTab(strt, stop, sTab)
  73. --> 4 4 first space char
  74.  
  75. strt, stop, sTab = string.find(myString, "[wpt]")
  76. disTab(strt, stop, sTab)
  77. --> 14 14 First char in w p t (brown)
  78.  
  79. strt, stop, sTab = string.find(myString, "[^wpt]")
  80. disTab(strt, stop, sTab)
  81. --> 1 1 First char not in wpt (T is not t)
  82.  
  83. strt, stop, sTab = string.find(myString, "o(.)")
  84. disTab(strt, stop, sTab)
  85. --> 13 14 w First match of 'o' followed by any one char (brown)
  86.  
  87. strt, stop, sTab = myString:find("o(.)")
  88. disTab(strt, stop, sTab)
  89. --> 13 14 w myString:find() instead of string.find(myString, )
  90.  
  91. strt, stop, sTab = string.find(myString2, "pl*")
  92. disTab(strt, stop, sTab)
  93. --> 15 15 First p in apple
  94.  
  95. strt, stop, sTab = string.find(myString2, "ck*")
  96. disTab(strt, stop, sTab)
  97. --> nil ck does not appear in string
  98.  
  99. strt, stop = string.find("bananas", 'az*')
  100. disTab(strt, stop, sTab)
  101. --> 2 2
  102.  
  103. strt, stop = string.find("bananas", 'an*')
  104. disTab(strt, stop)
  105. --> 2 3
  106.  
  107. strt, stop = string.find("bannnnnanas", 'an*')
  108. disTab(strt, stop)
  109. --> 2 7 Notice ending point of 7
  110.  
  111. strt, stop, sTab = string.find(myString2, "pl+")
  112. disTab(strt, stop, sTab)
  113. --> 16 17 First pl in apple
  114.  
  115. strt, stop = string.find("bananas", 'an+')
  116. disTab(strt, stop)
  117. --> 2 3 Same result as 'an*'
  118.  
  119. strt, stop = string.find("bannnnnanas", 'an+')
  120. disTab(strt, stop)
  121. --> 2 7 Same resukt as 'an*'
  122.  
  123. strt, stop, sTab = string.find(myString2, "pl-")
  124. disTab(strt, stop, sTab)
  125. --> 15 15 First p in apple
  126.  
  127. strt, stop = string.find("bananas", 'an-')
  128. disTab(strt, stop)
  129. --> 2 2 Compare to 'an*'
  130.  
  131. strt = string.find("bannnnnanas", 'an-')
  132. disTab(strt, stop)
  133. --> 2 2 Compare to 'an*'
  134.  
  135. strt, stop, sTab = string.find(myString2, "pl?")
  136. disTab(strt, stop, sTab)
  137. --> 15 15 First p in apple
  138.  
  139. strt, stop, sTab = string.find(myString, "ox", 20)
  140. disTab(strt, stop, sTab)
  141. --> 52 53 First match starting at position 20 from start
  142.  
  143. strt, stop, sTab = string.find(myString, "ox", -20)
  144. disTab(strt, stop, sTab)
  145. --> 52 53 First match starting 20 characters from the end
  146.  
  147.  
  148. -- string.format(formatstring, ..., target)
  149.  
  150. -- Returns a formatted version of its variable arguments
  151. -- following the description given in the first argument,
  152. -- which must be a string
  153.  
  154. -- formatstring - % and one or more of the following
  155. -- target - value(s) to be formatted
  156. -- Returns: A formatted string
  157.  
  158. -- Format specifiers must be preceded by a "%"
  159. -- c, d, E, e, f, g, i, o, u, X, and x all expect a number
  160. -- q and s expect a string
  161.  
  162. print("string.format()")
  163.  
  164. print(string.format("%c %d %E %e", 65, 2, 3, 4))
  165. -- prints char w/val 65, int, EXP, exp
  166.  
  167. print(string.format("%f %g", 5.67, 6))
  168. -- prints float, gen (int)
  169.  
  170. print(string.format("%i %o %u %u %X %x", 5.5, 15, -2, 2, 11, 11))
  171. -- prints whole num, octal, unsigned int, unsig int, HEX (upper), hex (lower)
  172.  
  173. num = 56
  174. newStr = string.format("%s %d", "The number is", num)
  175. print(newStr)
  176. --> The number is 56
  177.  
  178. num = 56
  179. newStr = string.format("The number is %d", num)
  180. print(newStr)
  181. --> The number is 56
  182.  
  183. ch = 'r'
  184. newStr = string.format("%s %s", "The char is", ch)
  185. print(newStr)
  186. --> The char is r "%s" prints a string
  187.  
  188. ch = 'r'
  189. newStr = string.format("%s %q", "The char is", ch)
  190. print(newStr)
  191. --> The char is "r" "%q" puts quotes around string
  192.  
  193. -- string.len(s)
  194.  
  195. -- Receives a string and returns its length
  196.  
  197. -- s - String to find length
  198. -- Returns: Length of the string s
  199.  
  200. print("string.len()")
  201.  
  202. strt = string.len(myString)
  203. print(strt)
  204. --> 66
  205.  
  206. strt = myString:len()
  207. print(strt)
  208. --> 66 Note format
  209.  
  210. -- string.lower(s)
  211.  
  212. -- Receives a string and returns a copy of this string
  213. -- with all uppercase letters changed to lowercase.
  214.  
  215. -- s - String to convert to all lower case
  216. -- Returns - Length of string s
  217.  
  218. print("string.lower()")
  219.  
  220. strt = string.lower(myString)
  221. print(strt)
  222. --> the quick brown fox jumped over the lazy dog. the fox jumped back.
  223.  
  224. strt = myString:lower()
  225. print(strt)
  226. --> the quick brown fox jumped over the lazy dog. the fox jumped back.
  227.  
  228. -- string.upper(s)
  229.  
  230. --Recieves a string and returns a copy of this string
  231. -- with all lowercase letters changes to uppercase.
  232.  
  233. -- s - String to convert to all upper case
  234.  
  235. -- Returns - Uppercase version of string s
  236.  
  237. print("string.upper()")
  238.  
  239. strt = string.upper(myString)
  240. print(strt)
  241. --> THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. THE FOX JUMPED BACK
  242.  
  243. strt = myString:upper()
  244. print(strt)
  245. --> THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. THE FOX JUMPED BACK
  246.  
  247. -- string.match(s, pattern, init)
  248.  
  249. -- Looks for the first match of pattern in the string s.
  250.  
  251. -- s - String to search
  252. -- pattern - pattern to match
  253. -- init - starting location in the string
  254.  
  255. -- returns - Captures from the first match of pattern
  256. -- in string s, or nil if none were found.
  257.  
  258. -- Note: position information is not available
  259.  
  260. print("string.match()")
  261.  
  262. strt = string.match(myString, "the")
  263. print(strt)
  264. --> the
  265.  
  266. strt = string.match(myString, "t(..)")
  267. print(strt)
  268. --> he
  269.  
  270. strt = string.match("I have 2 questions for you", "%a %a")
  271. print(strt)
  272. --> I h Match a char followd by a char
  273.  
  274. strt = string.match("I have 2 questions for you", "%a+ %a+")
  275. print(strt)
  276. --> I have Match any number of chars followed by any number of chars
  277.  
  278. strt = string.match("I have 2 questions for you", "%d+ %a+")
  279. print(strt)
  280. --> 2 questions Match one or more digits followed by one or more chars
  281.  
  282. strt = myString:match("b(....)")
  283. print(strt)
  284. --> rown
  285.  
  286. -- string.rep(s, n)
  287.  
  288. -- Returns a string that is the concatenation of n copies
  289. -- of the string s.
  290.  
  291. -- s - String to replicate
  292. -- n - int, number of times to replicate the string
  293.  
  294. -- Returns - n concatenations of string s
  295.  
  296. print("string.rep()")
  297.  
  298. strt = string.rep("abc", 5)
  299. print(strt)
  300. --> abcabcabcabcabc
  301. str = "abc"
  302. strt = str:rep(5)
  303. print(strt)
  304. --> abcabcabcabcabc
  305.  
  306. -- string.sub(s, i, j)
  307.  
  308. -- Returns the substring of s that starts at i
  309. -- and continues until j; i and j can be negative.
  310.  
  311. -- s - find substring of this string
  312. -- i - int, starting index
  313. -- j - int, ending index
  314.  
  315. -- Returns - Substring of string s
  316.  
  317. print("string.sub()")
  318.  
  319. strt = string.sub(myString, 5, 8)
  320. print(strt)
  321. --> quic
  322.  
  323. strt = myString:sub(5, 9)
  324. print(strt)
  325. --> quick
  326.  
  327. -- string.gsub(s, pattern, replace, n)
  328.  
  329. -- It can replace all instances of the pattern
  330. -- provided with the replace string.
  331.  
  332. -- s - Original string
  333. -- pattern - The string to replace
  334. -- replace - New string
  335. -- n - Used to limit the number of substitutions made
  336.  
  337. -- Returns - the modified string and the
  338. -- number of substitutions
  339.  
  340. print("string.gsub()")
  341.  
  342. strt, stop = string.gsub(myString, "fox", "mouse", 1)
  343. print(strt)
  344. print(stop)
  345. --> The quick brown mouse jumped over the lazy dog. The fox jumped back.
  346. --> 1
  347.  
  348. str = "123123123"
  349. strt, stop = str:gsub("2", "8")
  350. print(strt)
  351. print(stop)
  352. --> 183183183
  353. --> 3
  354.  
  355. str = "The quick {brown} fox jumped {over} the {lazy dog}."
  356. strt, stop = string.gsub(str,"{(.-)}", function(a) print(a) end)
  357. print(stop)
  358. --> brown
  359. --> over
  360. --> lazy dog
  361. --> 3
  362.  
  363. -- Note: This example used an anonymous function as the third parameter
  364. -- the function accepted each capture and printed it
  365.  
  366. str = "The quick {brown} fox jumped {over} the {lazy dog}."
  367. strt, stop = string.gsub(str,"{(.-)}", myFunc)
  368. print(strt)
  369. print(stop)
  370. --> brown
  371. --> over
  372. --> lazy dog
  373. --> The quick rep fox jumped rep the rep.
  374. --> 3
  375.  
  376. -- Note: Each capture is displayed by the function myFunc()
  377. -- myFunc returns "rep" which is substituted for
  378. -- the capture in the original string
  379. -- I replaced the function with the table sTab,
  380. -- which seems to be permitted. I was not able to
  381. -- display any table elements
  382.  
  383. -- string.gmatch(s, pat)
  384.  
  385. -- This returns a pattern finding iterator. The iterator will search through
  386. -- the string passed looking for instances of the pattern you passed.
  387.  
  388. -- s - The string to search
  389. -- pat - the pattern to match
  390.  
  391. -- Returns iterator
  392.  
  393. print("string.gmatch()")
  394.  
  395. for word in string.gmatch("Hello Codea user", "%a+") do
  396. print(word)
  397. end
  398.  
  399. -- Note: the use of gmatch in a for loop
  400.  
  401. strt = string.gmatch("Hello Codea user", "%a+")
  402.  
  403. for word in strt do
  404. print(word)
  405. end
  406.  
  407. --> Hello
  408. --> Codea
  409. --> user
  410.  
  411. -- Note: strt is a function returned by string.gmatch()
  412. -- Change "%a+" to "%a" and see how the output changes
  413. --
  414.  
  415. -- string.reverse(s)
  416.  
  417. -- Reverses a string
  418.  
  419. -- s - String to reverse
  420.  
  421. -- Returns - string reversed
  422.  
  423. print("string.reverse()")
  424.  
  425. strt = string.reverse("Hello Codea user")
  426. print(strt)
  427. --> resu aedoC olleH
  428.  
  429. str = "This is an example"
  430. strt = str:reverse()
  431. print(strt)
  432. --> elpmaxe na si sihT
  433.  
  434.  
  435.  
  436. end
  437.  
  438. function disTab(beg, lst, nTab)
  439. if beg ~= nil and lst ~= nil then
  440. print(beg.." "..lst)
  441. else
  442. print("nil")
  443. end
  444. if nTab ~= nil then
  445. print(nTab)
  446. end
  447.  
  448. end
  449.  
  450. function myFunc(value)
  451. print(value)
  452. return("rep")
  453. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement