Regular expression to allow only selected string values
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Regular expression to allow only selected string values




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Regular expression to allow only selected string values  
qazmlp


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-30-04 01:09 AM

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.





[ Post a follow-up to this message ]



    Re: Regular expression to allow only selected string values  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-30-04 01:09 AM

2004-09-29, 03:01(-07), qazmlp:
> Only either of the following values has to be allowed for a string variabl
e:
> 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





[ Post a follow-up to this message ]



    Re: Regular expression to allow only selected string values  
Laurenz Albe


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-30-04 01:09 AM

qazmlp <qazmlp1209@rediffmail.com> wrote:
> Only either of the following values has to be allowed for a string variabl
e:
> 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





[ Post a follow-up to this message ]



    Re: Regular expression to allow only selected string values  
Juhan Leemet


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-03-04 02:13 AM

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.






[ Post a follow-up to this message ]



    Re: Regular expression to allow only selected string values  
Paul Jarc


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-03-04 07: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:13 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register