3
votes

I'm looking for a way search in a string for a very specific set of characters: "(),:;<>@[\]

specialChar = str:find("[\"][%(][%)][,][:][;][<][>][@][%[][%]][\\]")

I'm thinking that there will be no pattern to satisfy my need because of the Limitations of Lua patterns.

I have read the Lua Manual pattern matching section pretty thoroughly but still can't seem to figure it out.

Does anyone know a way I can identify if a given string contains any of these characters?

Note, I do not need to know anything about which character or where in the string it is, if that helps.

1
Lua Reference Manual - 6.4.1 - Patterns: "[set]: represents the class which is the union of all characters in set. (...)". In other words your pattern would match the very sequence you have specified. If you want any of them, put them together in [] as in Wiktor's answer. Also - don't assume that there is no solution straight away. Having good mindset is often helpful in finding answers. Please note that it works exactly the same way in regex. - Green
@Green thanks, I didn't assume straight away I have been trying for a couple of hours to work out the right pattern, I just used my latest attempt as an example - Gamora
Hmm, maybe "straight away" was a little bit too much. Still the link to "Limitations of Lua patterns" suggests that you have more or less given up. The fact that the link itself contains examples and explanations that would help you strengthens this feeling. Anyway, it's not that important after all, it was just a simple suggestion or encouragement. - Green
@Green I appreciate that, pattern matching seems to be the main area of Lua that is still alluding me! - Gamora

1 Answers

5
votes

To check if a string contains ", (, ), ,, :, ;, <, >, @, [, \ or ] you may use

function ContainsSpecialChar(input)
    return string.find(input, "[\"(),:;<>@[\\%]]")
end

See the Lua demo