[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: PatternMatching symbol for magic characters :-)
- From: Coda Highland <chighland@...>
- Date: Wed, 17 Jun 2015 19:41:06 -0700
On Wed, Jun 17, 2015 at 7:30 PM, voidptr69@hotmail.com
<voidptr69@hotmail.com> wrote:
> On 2015-06-15 8:37 PM, Roberto Ierusalimschy wrote:
>
> This wouldn't quite be the same thing though, would it? I'm not seeing a
> very specific definition of "punctuation" in PIL or the manual, but maybe
> I'm missing it. Implementation detail? But at the very least, '%p' would
> almost certainly match commas, and '%W' would include control characters,
> white space, and other random things, right? Neither of those are quite a
> replacement for the proposed '%m' semantics.
>
> Why would someone need '%m'? The motivation presented here was to
> escape the magic characters. But Lua ensures that it is safe to escape
> any non-alphanumeric character, not only the magic ones. So, you can
> escape anything that matches '%p' to have a valid, non-escaped pattern:
>
> p = string.gsub(p, "%p", "%%%0")
>
> No need for '%m'.
>
> -- Roberto
>
>
> :o) I guess I missed that line reading the manual
>
> s2 = string.gsub(s, "[%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%0")
> s1 = string.gsub(s, "%p", "%%%0")
>
> I was in the mood to do something like the first line and thought it could
> be nice and more clean to have a symbol for the magic subset....
>
> :-)
>
You don't need to escape special characters inside a character class except for:
], which has to be expressed as %]
%, which has to be expressed as %%
-, which doesn't have to be escaped if you put it at the beginning
or the end of the class
^, which doesn't have to be escaped if it's anywhere but the beginning
In other words, that code is equivalent to:
s2 = string.gsub(s, "[%%^$().[%]*+?-]", "%%%0")
/s/ Adam