Saturday, April 21, 2012

Lua pattern matching

Hi,

i'm having a bit of difficulty getting a pattern to work. I thought what i wanted to do was possible, although now not 100% sure,

I'm trying to create a pattern that will match any string containing one word, for example 'port', unless that occurs in a word from a list of safe-words like 'imPORTant' and 'supPORT'. is it actually possible with pattern matching?

Thanks

stew|||Something like :


Code:



local strSrch = "Any port in a storm.";
local strBeg, strEnd, strCap = string.find(strSrch, ".*%s+(port)%s+.*");

This won't find "Port" with any capital letters, and only finds the word when surrounded by at least one space on either side - so it also wont find "port." (i.e. with a full stop after it)



".*[%s%-][pP][oO][rR][tT][%s%.].*"

...might be more universal...

Not sure off the top of my head, but the first pattern should be reasonable if you are sure it isn't capitalised and is surrounded by spaces.

You should check out the following web site ;)

http://www.lua.org/pil/20.2.html

No comments:

Post a Comment