Vex is a Lua module which implements the Verbal Expressions standard in order to simplify regular expressions to common language.
First, install the lrexlib-pcre module. This is usually done with LuaRocks.
luarocks install lrexlib-pcre
Then, require the vex module in your project.
vex = require("vex")
After that, all you need to do is create a new vex object and you can start working with it!
Here are a few examples to show you how it all works.
Here, we test to see if a URL is valid. Note that each method in the vex object returns itself, allowing you to chain methods together.
tester = vex():startofline():find("http"):maybe("s"):find("://"):maybe("www."):anythingbut(" "):endofline()
testURL = "https://github.com"
print("Testing \n\t" .. tester.pattern .. "\nagainst\n\t" .. testURL .. "\n")
res = tester:match(testURL)
print(res and "Valid!" or "Invalid!")
Here, we use a vex object's gsub method to replace "bird" with "duck".
repStr = "Replace a bird with a duck"
print(repStr)
res = vex():find("bird"):gsub(repStr, "duck")
print(res)
Vex also supports captures and has methods to specify how many of an object or group you want.
str = "cat and dog and fish and buffalo buffalo buffalo buffalo buffalo buffalo"
tester = vex():begincapture():find("cat"):endcapture():find(" and "):begincapture():find("dog"):endcapture():find(" and "):begincapture():find("fish"):endcapture():find(" and "):begincapture():begingroup():find("buffalo"):maybe(" "):endgroup():nomorethan(3):endcapture():anything()
cat, dog, fish, buffalo = tester:match(str)
print(cat, dog, fish, buffalo)
Much like the Ruby implementation of Verbal Expressions, the or method has been implemented as alternatively due to or being a keyword in Lua. For the same reason, then is not implemented.
If you'd like to contribute, please do. Feel free to clone, fork, or send a pull request. This module is here for everyone!