[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: |
Eric Pruitt |
Subject: |
Re: [bug-gawk] How to escape a string into its regex form |
Date: |
Sat, 7 Sep 2019 21:19:43 -0700 |
User-agent: |
NeoMutt/20170113 (1.7.2) |
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