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: arnold
Subject: Re: [bug-gawk] How to escape a string into its regex form
Date: Sat, 07 Sep 2019 23:45:02 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Eric's approach is one way.

Another way to see if one string matches another, literal string,
is to use index() instead of regular expression matching.

Arnold

Eric Pruitt <address@hidden> wrote:

> On Sat, Sep 07, 2019 at 10:39:28PM -0500, Peng Yu wrote:
> > For example, for a string "abc.xyz", the escaped string will be
> > "abc\.xyz". What is the correct way to perform such an escape for any
> > arbitrary string? Thanks.
>
> I use "[...]" for everything other than "\" because I ran into some
> portability issues with various AWK implementations when I tried to use
> "\" to escape characters in regular expressions. Here's the function I
> wrote:
>
>     # Escape a string so that it will be interpreted as a literal value
>     # when used in a regular expression.
>     #
>     # Arguments:
>     # - string: String to escape.
>     #
>     # Returns: An escaped string.
>     #
>     function regex_quote(string)
>     {
>         # Brackets are used for escaping most symbols to avoid problems
>         # caused by differences in how "\" escapes are handled depending on
>         # the context and AWK interpreter.
>         gsub(/\\/, "\\\\", string)
>         gsub(/[\135\133$^*()+{}|.?]/, "[&]", string)
>
>         return string
>     }
>
> Eric



reply via email to

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