Unix Shell - Regular expression to allow only selected string values

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2004 > Regular expression to allow only selected string values





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Regular expression to allow only selected string values
qazmlp

2004-09-29, 8:09 pm

Only either of the following values has to be allowed for a string variable:
FIRST
SECOND
THIRD

What could be the regular expression to check this?
"^FIRST|SECOND|THIRD$" does not seem to work.
Stephane CHAZELAS

2004-09-29, 8:09 pm

2004-09-29, 03:01(-07), qazmlp:
> Only either of the following values has to be allowed for a string variable:
> FIRST
> SECOND
> THIRD
>
> What could be the regular expression to check this?
> "^FIRST|SECOND|THIRD$" does not seem to work.


Try:

^(FIRST|SECOND|THIRD)$

Note that it's an extended regular expression syntax as in egrep
or grep -E.

If you want to match a shell variable, use case instead:

case $var in
FIRST|SECOND|THIRD) echo OK;;
*) echo NOK;;
esac

--
Stephane
Laurenz Albe

2004-09-29, 8:09 pm

qazmlp <qazmlp1209@rediffmail.com> wrote:
> Only either of the following values has to be allowed for a string variable:
> FIRST
> SECOND
> THIRD
>
> What could be the regular expression to check this?
> "^FIRST|SECOND|THIRD$" does not seem to work.


It looks good though.

Please quote the complete command that does not work as expected
(as in 'sed -e "/^FIRST|SECOND|THIRD$/d"') and what you want it to do.

Yours,
Laurenz Albe
Juhan Leemet

2004-10-02, 9:13 pm

On Wed, 29 Sep 2004 10:23:53 +0000, Laurenz Albe wrote:
> qazmlp <qazmlp1209@rediffmail.com> wrote:
>
> It looks good though.
>
> Please quote the complete command that does not work as expected
> (as in 'sed -e "/^FIRST|SECOND|THIRD$/d"') and what you want it to do.


Hmm, when I tried this in bash on SuSE Linux, I found that I had to escape
the '|' characters. I get confused with when to escape and when not to
escape. I found that this works:

sed -e "s/^\(FIRST\|SECOND\|THIRD\)$/-match-/"

I also had to bracket the alternation with parens, otherwise it seems that
the beginning of line caret applied only to FIRST? and the end of line $
applied only to THIRD? BTW, it does not matter whether the quotes are
single (') or double ("). I guess the '|' is considered a normal character
unless escaped? like the parens "()". Maybe one of these days I'll get it
straight (but I'll always test to make sure!).

--
Juhan Leemet
Logicognosis, Inc.

Paul Jarc

2004-10-03, 2:48 am

Juhan Leemet <juhan@logicognosis.com> wrote:
> Hmm, when I tried this in bash on SuSE Linux, I found that I had to escape
> the '|' characters. I get confused with when to escape and when not to
> escape. I found that this works:
>
> sed -e "s/^\(FIRST\|SECOND\|THIRD\)$/-match-/"


That escaping is needed for sed, not for the shell. You can see what
sed saw like this:
$ printf %s\\n "s/^\(FIRST\|SECOND\|THIRD\)$/-match-/"

> I also had to bracket the alternation with parens, otherwise it seems that
> the beginning of line caret applied only to FIRST? and the end of line $
> applied only to THIRD?


Right: concatenation (such as between "^" and "F") binds things
together more strongly than alternation.

> BTW, it does not matter whether the quotes are single (') or double
> (").


True in this case. In general, it's easier to use single quotes if
you aren't doing any substitution. In double quotes, "\" and "$" are
sometimes special to the shell, depending on what comes after them.

> I guess the '|' is considered a normal character unless escaped?
> like the parens "()".


Yes; sed uses basic regular expressions. For extended regular
expressions, "|()" are special when unescaped, and literal when
escaped. You also get some extras like "+" with extended regular
expressions.


paul
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com