User:OrenBochman/Lua Strings Advanced

Pattern Matching with Regular Expressions edit

Lua supports string matching using regualr expressions. Regular expressions are defined with the following symbols:


  • ( - start group
  • ) - end group
  • . - any single character
  • % - escape character
  • + - 1 or more greedy
  • * - 0 or more greedy
  • - - 0 or more not greedy
  • ? - 0 or one
  • [ - start range
  • ] - end range
  • ^ - string start
  • $ - string end

Character Classes edit

There are some predefined ranges:

  • . - all characters
  • %a - letters
  • %l - lower case
  • %u - upper case
  • %d - digits
  • %h - hexadecimal digits
  • %w - alphanumeric characters
  • %p - punctuation characters
  • %s - whitespace
  • %c - control characters

upper case version of these is the complement range.

s = "Deadline is 30/05/1999, firm"
date = "%d%d/%d%d/%d%d%d%d"
print(string.sub(s, string.find(s, date)))   --> 30/05/1999


print(string.gsub("hello, up-down!", "%A", ".")) --> hello..up.down. 4

here 4 is the count of substitutions returned by gsub

to count the number of Vowel in a string

dummy, nvow = string.gsub(text, "[AEIOUaeiou]", "")

where the dummy variable holds the vowel free string and nvow has the result.