Lua Regular Expressions
Archive - Originally posted on "The Horse's Mouth" - 2009-08-28 12:47:34 - Graham Ellis
You'll find an example of Lua pattern matching here in our courseware examples. A Lua pattern is a string that may include:
• Literal Characters to match exactly - e.g.
Letters, digits and many special chars match exactly
%% means really match a % character (similar for other specials)
• Character groups to match any one character from a group - e.g.
[aeiou] for a lower case vowel
%d means a digit
%a for a letter
%u for an upper case letter
%l for a lower case letter
%s for a space
%S for a non-space (and other capitalisations for opposite groups)
• Anchors to say if you're looking at the start or end of a string - e.g.
^ means "starting with"
$ means "ending with"
• Counts to say how many times to match the previous character - e.g.
+ means one or more of the previous item
? means zero or 0ne character
* means 0 or more or the previous item
- also means 0 or more of the previous item but is SPARSE
• Capture parenthesis to indicate interesting bits to save
There are some examples of iterating through a string to match patterns in Lua here.
Lua's pattern matching is covered on both our Learning to Program in Lua and Lua Programming courses. Illustration - on a private Lua course