This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Selim's profile picture

Check if Character is a "Magic Character"

Started by Selim, 20 May 2016 - 12:32 PM
Selim #1
Posted 20 May 2016 - 02:32 PM
Is there a good way to check if a character is one of the "magic characters" (().%+-*?[]^$) used in string patterns? I am trying to use
string.find("().%+-*?[]^$",char)
except if it is a "magic character", it will error as the pattern is invalid.

My latest attempt uses pcall, but I was wondering if there is a better way.
Edited on 20 May 2016 - 12:39 PM
powerboat9 #2
Posted 20 May 2016 - 02:58 PM
The pattern is invalid, as you have to escap some charicters:

"%(%)%.%%%+%-%*%?%[]%^%$"

Almost all of the charicters you're searching for are special charicters in patterns. "%" is like a backslash in patterns.
The Higher Realm #3
Posted 20 May 2016 - 03:06 PM
I can't test it right now but I believe you can use "%" to escape it like
string.find(char, %.) –Checks for dot/period
Edited on 20 May 2016 - 01:47 PM
Dragon53535 #4
Posted 20 May 2016 - 03:11 PM
Why not just have a table with the characters you want to know about as keys to true?


local tbl = { ["{"] = true, ["["] = true, ["%"] = true} --#Etc
--#Might be easier to use a string.char loop though.
if tbl[char] then
  --#One of the magic chars
else
  --#not
end

Edit:

Code for loop with ascii values, faster to type :P/>


local textTbl = {36,37,40,41,42,43,45,46,63,91,93,94}
--#	$%()*+-.?[]^
local charTbl = {} --#This table is the one you would use
for a,v in ipairs(textTbl) do
  charTbl[string.char(v)] = true
end


Edit2:

I can't test it right now but I believe you can use "%" to escape it like
string.find(char, %.) –Checks for dot/period
I think your big problem is your arguments are backwards. It's string.find(string, pattern)
He's looking for a single character, the way he's got it would be technically correct if he escapes char. Mind you if he escapes say the letter w, then he might have different problems.
Edited on 20 May 2016 - 01:20 PM
Selim #5
Posted 20 May 2016 - 03:30 PM
–snip–
–snip–
I am checking to determine if it is a "magic character", not fix the pattern.

–snip–
Yea, that is what I was assuming, but I really didn't want to have to do it that way, it gets really annoying to type out the table. But, as you did it for me…. :)/>
Edited on 20 May 2016 - 01:32 PM
Dragon53535 #6
Posted 20 May 2016 - 03:48 PM
I would take the second option I gave, as you just have to deal with the decimal representation of the binary values for each character. Makes it easier to type, plus if you need it for elsewhere just use something like this and grab the number you need.
CoderPuppy #7
Posted 20 May 2016 - 07:24 PM
string.find("().%+-*?[]^$", char, 1, true)

If the third argument is true it isn't interpreted as a pattern.
Selim #8
Posted 20 May 2016 - 07:35 PM
Ohh, thanks! That makes it much easier.

EDIT: Oddly enough, that doesn't seem to work.
string.match("().%+-*?[]^$","%",1,true)
malformed pattern (ends with '%')

EDIT2: I'm dumb, you linked to string.find and used string.find in your example. Whoops.
Edited on 20 May 2016 - 05:47 PM