Lua regex / string helpers

Match empty parenthesis pair, such as () or ( ), you use the pattern ‘%(%s*%)’
‘[0-9]’ is simpler when written as ‘%d’, ‘[0-9a-fA-F]’ is the same as ‘%x’
.                    all characters
%a                letters
%c                control characters
%d                digits
%l                 lower case letters
%p                 punctuation characters
%s                 space characters
%u                 upper case letters
%w                 alphanumeric characters
%x                 hexadecimal digits
%z                 the character with representation 0
‘[^n]’ matches any character different from newline.
`^´: ‘[^0-7]’ finds any character that is not an octal digit
+                 1 or more repetitions
*                 0 or more repetitions
–                 also 0 or more repetitions
?                 optional (0 or 1 occurrence)
ex: tmpString = extract this
print(string.tmpString(tmpString, (.-))
output : extract this
The character `%´ works as an escape for those magic characters. So, ‘%.’ matches a dot; ‘%%’ matches the character `%´ itself. You can use the escape `%´ not only for the magic characters, but also for all other non-alphanumeric characters. When in doubt, play safe and put an escape.

Leave a Reply

Your email address will not be published. Required fields are marked *