Which characters are included in the Lua punctuation string pattern (%p)?
Asked Answered
S

3

11

I haven't been able to find documentation of which characters compound the punctuation set "%p" in Lua.

Saxe answered 13/6, 2014 at 0:47 Comment(0)
P
11

The answer is locale dependent, it is a direct interface to the C function.
Actually, if there is a C standard function which does something similar to the Lua function, it is near-certain that the Lua function just wraps the C function, warts and all, even without looking at the specific case.
(This is part of the reason file:read() still has trouble reading text with embedded zeroes in 5.2, maybe even will have in 5.3)

While Amaden gave a good answer for the "C" locale, and ColonelThirtyTwo gave the right way to check for the current locale, the C standard only says:

ispunct(): The ispunct function tests for any printing character that is one of a locale-specific set of punctuation characters for which neither isspace nor isalnum is true. In the "C" locale, ispunct returns true for every printing character for which neither isspace nor isalnum is true.

Pharisaism answered 13/6, 2014 at 1:3 Comment(0)
C
9

%p is matched by the C function ispunct (C source v 5.2), which matches the following:

041 ‘‘!’’     042 ‘‘ ’’       043 ‘‘#’’       044 ‘‘$’’       045 ‘‘%’’ 
046 ‘‘&’’     047 ‘‘’’’       050 ‘‘(’’       051 ‘‘)’’       052 ‘‘*’’ 
053 ‘‘+’’     054 ‘‘,’’       055 ‘‘-’’       056 ‘‘.’’       057 ‘‘/’’ 
072 ‘‘:’’     073 ‘‘;’’       074 ‘‘<’’       075 ‘‘=’’       076 ‘‘>’’ 
077 ‘‘?’’     100 ‘‘@’’       133 ‘‘[’’       134 ‘‘\’’       135 ‘‘]’’ 
136 ‘‘^’’     137 ‘‘_’’       140 ‘‘‘’’       173 ‘‘{’’       174 ‘‘|’’ 
175 ‘‘}’’     176 ‘‘~’’

(From man ispunct)

Calabash answered 13/6, 2014 at 0:57 Comment(0)
P
9

A small script to find them:

for i=0,255 do
    if string.match(string.char(i), "%p") then
        io.write(string.char(i))
    end
end
io.write("\n")

-- $ luajit test.lua
-- !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Promenade answered 13/6, 2014 at 0:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.