Since there is no usual
word boundary in Lua, you can make use of a frontier pattern %f
. %f[%a]
matches a transition to a letter and %f[%A]
matches the opposite transition.
%f[set]
, a frontier pattern; such item matches an empty string at any position such that the next character belongs to set and the previous character does not belong to set. The set set is interpreted as previously described. The beginning and the end of the subject are handled as if they were the character
.
You can use the following ContainsWholeWord
function:
function ContainsWholeWord(input, word)
return string.find(input, "%f[%a]" .. word .. "%f[%A]")
end
print(ContainsWholeWord("Info Playlist pause","Play") ~= nil)
print(ContainsWholeWord("Info Play List pause","Play") ~= nil)
See IDEONE demo
To fully emulate
behavior, you may use
"%f[%w_]" .. word .. "%f[^%w_]"
pattern, as
matches the positions between:
- Before the first character in the string, if the first character is a word (
[a-zA-Z0-9_]
) character.
- After the last character in the string, if the last character is a word (
[a-zA-Z0-9_]
) character.
- Between two characters in the string, where one is a word character (
[a-zA-Z0-9_]
) and the other is not a word character ([^a-zA-Z0-9_]
).
Note that %w
Lua pattern is not the same as w
since it only matches letters and digits, but not an underscore.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…