how to check if a word appears as a whole word in a string in Lua
Asked Answered
D

2

8

not sure how to check if a word appears as a whole word in a string, not part of a word, case sensitive. for example:

Play is in strings

Info Playlist Play pause

but not in the strings

Info Playlist pause
Info NowPlay pause
Dissimilar answered 29/9, 2015 at 19:51 Comment(5)
Why is it not in Info Playlist pause?Egalitarian
Well, that's the goal. I only want it to be true if "Play" appears a whole word in a string, not part of a word.Dissimilar
@stribizhev I don't see how this is a duplicate of the question you marked it as - this question is about matching a word boundary.Sirup
I actually even did not want to mark it as a duplicate, sorry, I meant to just show this link.Brunell
Thank you, but I just realized that the question should be case sensitive instead of case insensitiveDissimilar
L
19

Since there is no usual \b 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 \0.

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 \b behavior, you may use

"%f[%w_]" .. word .. "%f[^%w_]"

pattern, as \b 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.

Laquanda answered 29/9, 2015 at 21:47 Comment(1)
Frontier patterns are supported since LUA 5.2.Pernick
S
1
function isWordFoundInString(w,s)
  return select(2,s:gsub('^' .. w .. '%W+','')) +
         select(2,s:gsub('%W+' .. w .. '$','')) +
         select(2,s:gsub('^' .. w .. '$','')) +
         select(2,s:gsub('%W+' .. w .. '%W+','')) > 0
end

print(isWordFoundInString('Play','Info Playlist Play pause'))
print(isWordFoundInString('Play','Info Playlist pause'))
print(isWordFoundInString('Play','Info NowPlay pause'))
Smitherman answered 29/9, 2015 at 22:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.