bug-bash
[Top][All Lists]
Advanced

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

Re: How to match regex in bash? (any character)


From: Stephane CHAZELAS
Subject: Re: How to match regex in bash? (any character)
Date: Mon, 3 Oct 2011 12:01:34 +0000 (UTC)
User-agent: slrn/pre1.0.0-18 (Linux)

2011-10-03, 13:48(+02), Andreas Schwab:
> Stephane CHAZELAS <address@hidden> writes:
>
>> The problem and confusion here comes from the fact that "\" is
>> overloaded and used by two different pieces of software (bash
>> and the system regex).
>
> That's nothing new.  The backslash is widely used as a quote character
> in several languages, which requires two levels of quoting if one of
> these languages is embedded in another one.
[...]

Yes, but in this case, contrary to zsh doesn't do two levels of
quoting. Bash quoting means to escape the RE operators, and
that's where the problem comes from. For it to work fully, bash
would need to implement the full RE parsing to know where to put
backslashes when characters are quoted.

Bash turns:

"." to \. before calling the regex(3) API
'[.]' to \[\.\] (fine)
['.'] to [\.] (not fine)
['a]'] to [a\]] (not fine)
(.)\1 to (.)1 (fine or not fine depending on how you want to
              look at it)
(?i:test} to (?i:test) (assuming regex(3) are implemented with
             PCREs: fine or not fine depending on how you want
             to look at it).

In zsh, it's simpler as quoting just quotes shell characters, it
doesn't try to escape regexp operators.

-- 
Stephane




reply via email to

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