[gnu.bash.bug] regular expressions

maart@cs.vu.nl (Maarten Litmaath) (08/25/89)

I've always wanted to be able to do something like the following:

	case $domain in
	\(.*\)\.\(.*\)\.edu)
		department=\1
		university=\2
	*)
		# ...
	esac

instead of

	case $domain in
	*.*.edu)
		department=`echo "$domain" | sed 's/\([^.]*\).*/\1/'`
		university=`echo "$domain" | sed 's/.*\([^.]*\).edu/\1/'`
	*)
		# ...
	esac

You get the idea.  Is this possible with bash?
-- 
"rot H - dD/dt = J, div D = rho, div B = 0, |Maarten Litmaath @ VU Amsterdam:
rot E + dB/dt = 0" and there was 7-UP Light.|maart@cs.vu.nl, mcvax!botter!maart

bfox@AUREL.CALTECH.EDU (Brian Fox) (08/30/89)

   Date: 25 Aug 89 16:12:23 GMT
   From: maart@star.cs.vu.nl  (Maarten Litmaath)
   Organization: V.U. Informatica, Amsterdam, the Netherlands
   Sender: bug-bash-request@prep.ai.mit.edu

   I've always wanted to be able to do something like the following:

	   case $domain in
	   \(.*\)\.\(.*\)\.edu)
		   department=\1
		   university=\2
	   *)
		   # ...
	   esac

   instead of

	   case $domain in
	   *.*.edu)
		   department=`echo "$domain" | sed 's/\([^.]*\).*/\1/'`
		   university=`echo "$domain" | sed 's/.*\([^.]*\).edu/\1/'`
	   *)
		   # ...
	   esac

   You get the idea.  Is this possible with bash?

Well, not without changing the semantics of the "case" command, and
writing a regular expression parser into the shell.

The pattern matcher is a globber, not a RE parser.  Sorry.

Brian

maart@cs.vu.nl (Maarten Litmaath) (08/30/89)

bfox@AUREL.CALTECH.EDU (Brian Fox) writes:
\...	   case $domain in
\	   \(.*\)\.\(.*\)\.edu)
\		   department=\1
\		   university=\2
\	   *)
\		   # ...
\	   esac
\...
\Well, not without changing the semantics of the "case" command, and
\writing a regular expression parser into the shell.
\
\The pattern matcher is a globber, not a RE parser.  Sorry.

Of course; it was never my intention to use the (normal) `case' command for
my idea.  But perhaps a syntax extension would be worthwhile?

	ecase ... in
	...
	esace
[sic]
-- 
C, the programming language that's the same |Maarten Litmaath @ VU Amsterdam:
         in all reference frames.           |maart@cs.vu.nl, mcvax!botter!maart

rayan@cs.toronto.edu (Rayan Zachariassen) (08/30/89)

>   Date: 25 Aug 89 16:12:23 GMT
>   From: maart@star.cs.vu.nl  (Maarten Litmaath)
>   Organization: V.U. Informatica, Amsterdam, the Netherlands

>   I've always wanted to be able to do something like the following:

>	   case $domain in
>	   \(.*\)\.\(.*\)\.edu)
>		   department=\1
>		   university=\2
>	   *)
>		   # ...
>	   esac

>   instead of ...

You will obviously like the ZMailer configuration language: a modern shell
with exactly this extension (originally in the case statement but it will
be a 'sift' statement in the next version).  Oh, and the way to write it
is

sift $domain in
(.)\.(.)\.edu)
	department=\1
	university=\2
	;;
tfis

because I use token-based REs.

rayan

bet@ORION.MC.DUKE.EDU (Bennett Todd) (09/19/89)

>   case $domain in
>   \(.*\)\.\(.*\)\.edu)
>	   department=\1
>	   university=\2
>   *)
>	   # ...
>   esac

Hmm. I don't mind the current notion of "case" using globbing rather
than r.e. matching. expr(1) makes r.e. substitution available easily
enough. I'd rather see expr(1) built in for performance reasons (like
echo, test, etc.) than see the definition of the case construct changed,
or see another extended case construct added.

By the way, I would code the above example:

	department=`expr $domain : '\([^.]*\)\..*$'`
	university=`expr $domain : '[^.]*\.\([^.]*\)\..*$'

-Bennett
bet@orion.mc.duke.edu