[H-GEN] regular expressions

Jason Henry Parker jasonp at uq.net.au
Sat Aug 19 04:48:35 EDT 2000


[ Humbug *General* list - semi-serious discussions about Humbug and ]
[ Unix-related topics.  Please observe the list's charter.          ]

Michael Anthon <michael at anthon.net> writes:

> jason wrote:
> > I always trap my sed expressions in forward-quotes to hide them
> > completely from the shell.  [ObPeeve:  forward quotes cannot be
> > escaped from bash _at all_.]
> > 
> I'm reading between the lines here a bit, so if I am misinterpreting,
> then I apologise....

Yes, my writing was unclear.  What I meant to point out is that "word"
is subject to a level of shell expansion, whereas 'word' is not.  I
thought I'd found a case long ago where '' was more effective than "",
but experimentation isn't bearing me out.

Hrm.  The bash manual page (GNU bash, version 2.04.0(1)) says:

       Enclosing  characters in  double quotes  preserves  the literal
       value of  all characters within the quotes,  with the exception
       of $,  `, and \.  The  characters $ and `  retain their special
       meaning  within  double  quotes.   The  backslash  retains  its
       special  meaning only  when followed  by one  of  the following
       characters: $,  `, ", \, or  <newline>.  A double  quote may be
       quoted within double quotes by preceding it with a backslash.

So it would appear that for *most* regexes, double-quoting will
suffice.  To see where it might *possibly* be a problem, consider:

$ sed -e's/\\/slash/g'
\
slash
\\
slashslash
$ sed -e"s/\\/slash/g"
sed: -e expression #1, char 11: Unterminated `s' command
$ sed -e"s/\\\\/slash/g"
\
slash

>        \+      Matches the single character regular expression or

Well, I'll be.  I thought sed didn't have the + metacharacter.  It
would appear it does, but requires a \ in front, unlike the very
similar * metacharacter.  How annoying.

jason
-- 
``I worry that you'll work in an office ... have children ... celebrate
wedding anniversaries ... the world of heterosexuals is a sick and
boring life!''  --  Aunt Ida

--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.



More information about the General mailing list