Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a pattern to only match a specific set of characters?

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.

like image 497
Gamora Avatar asked Sep 05 '25 03:09

Gamora


1 Answers

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

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

See the Lua demo

like image 88
Wiktor Stribiżew Avatar answered Sep 07 '25 21:09

Wiktor Stribiżew