[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: regex subexpressions broken
From: |
Fran Litterio |
Subject: |
Re: regex subexpressions broken |
Date: |
Fri, 11 May 2007 11:43:12 -0400 |
Ortwin wrote:
[[ "a b c" =~ "a (.) c" ]]
echo ${BASH_REMATCH[1]}
Recently, a change was made to cause quotes to suppress the special meaning
of regular expression characters in the right-hand side of =~. This is how
to do the above now:
[[ "a b c" =~ a\ (.)\ c ]]
I submitted a patch to the Bash man page describing these new quoting
semantics. Just in case it was missed, I've included it again (below).
--
Francis Litterio
--- bash.1.orig 2006-10-03 08:54:26.000000000 -0400
+++ bash.1 2007-03-24 17:15:06.459699200 -0400
@@ -686,8 +686,19 @@
.if n .sp 1
An additional binary operator, \fB=~\fP, is available, with the same
precedence as \fB==\fP and \fB!=\fP.
-When it is used, the string to the right of the operator is considered
-an extended regular expression and matched accordingly (as in
\fIregex\fP(3)).
+When this operaor is used, the string to the right of the operator is
+considered an extended regular expression and matched accordingly (as
+in \fIregex\fP(3)). If the regular expression string is quoted with
+either single or double quotes, the special meaning of the regular
+expression characters (such as '.', '*', '+', '[', ']', '(', ')',
+etc.) is suppressed. Thus, if the regular expression string contains
+whitespace, it is best to escape the whitespace characters using '\\'
+instead of quoting the entire regular expression string, as follows:
+.if t .sp 0.5
+.if n .sp 1
+ \fB[[\fP "$DATA" \fB=~\fP foo\\ bar.*bletch \fB]]\fP
+.if t .sp 0.5
+.if n .sp 1
The return value is 0 if the string matches
the pattern, and 1 otherwise.
If the regular expression is syntactically incorrect, the conditional