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: Wolfgang Laun
Subject: Re: [bug-gawk] How to escape a string into its regex form
Date: Sun, 8 Sep 2019 09:30:34 +0200

"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.

Wolfgang

On Sun, 8 Sep 2019 at 07:45, <address@hidden> wrote:

> 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]