bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] How to escape a string into its regex form


From: Peng Yu
Subject: Re: [bug-gawk] How to escape a string into its regex form
Date: Sun, 8 Sep 2019 10:52:04 -0500

Here is what I come up. Not sure if it is the best fix.

function str2regex(s) {
        gsub(/[][$*()+{}|.?/]/, "[&]", s);
        gsub(/\^/, "\\\\&", s);
        return s
}


On 9/8/19, Peng Yu <address@hidden> wrote:
> On 9/8/19, Wolfgang Laun <address@hidden> wrote:
>> "match the string *exactly*"
>> What's wrong with "=="?
>>
>> The gsub for enclosing metacharacters in brackets might also be written
>> as:
>>    gsub( /[][$^*()+{}|.?/]/, "[&]", regex );
>> There's no need to use octal escapes.
>> And, just to be on the safe side, I'd include the solidus, in case
>> someone
>> wants this to generate code, i.e., the result should appear verbatim in
>> an
>> awk program.
>
> This does not deal with ^ correctly. What is the best way to fix it?
> Thanks.
>
> --
> Regards,
> Peng
>


-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]