I need to detect a word say john from a larger string, say john is a good man So I have this pattern to find the word
[j][o][h][n]
and use it as follows:
local pattern = "[j][o][h][n]"
local str = "john is a good man"
print(str:find(pattern))
In order to accept if letters occur multiple times(e.g jooohn is a good man), I modify the pattern as
[j]+[o]+[h]+[n]+
I need to ignore spaces(or other non alphabetic chars) which may occur in between the word(e.g joh;n is a good man), so I add [^a-z]* to each letter and the result pattern is
[j]+[^a-z]\*[o]+[^a-z]\*[h]+[^a-z]\*[n]+[^a-z]\*
Now this is perfect for a word like "jooh!n is a good man". But if the ! symbol occurs within the repetiting letter, the pattern fails. For example, in jo!ohn is a good man the pattern is not detected. How do I modify my pattern in order to achieve this?
EDIT
Example:
local str = "jooohn is a good man"
local pattern = "[j]+[^a-z]*[o]+[^a-z]*[h]+[^a-z]*[n]+[^a-z]*"
print(str:find(pattern))
this will print
1 7
But
local str = "joo!ohn is a good man"
local pattern = "[j]+[^a-z]*[o]+[^a-z]*[h]+[^a-z]*[n]+[^a-z]*"
print(str:find(pattern))
will print only nil
I need a pattern which allows me to detect joo!ohn in scenario 2
This should do "j[j%W]*o[o%W]*h[h%W]*n[n%W]*"
:
local str = "joo!ohn is a good man"
local pattern = "j[j%W]*o[o%W]*h[h%W]*n[n%W]*"
print(str:find(pattern))
prints 1 8
.