[comp.lang.perl] a regular expression question

shani@TAURUS.BITNET (05/27/90)

Hi. I hope someone will help me to figure this out:

When I use the matching operator on something like

"cp" =~ /cp\s*@*\s*/

it matches, and I think it shouldn't because as far as I understand,
only expressions of the form "cp\s*@*\s*" (that is -
cp\s<anything>@<anything>\s<anything> ) should have matched the pattern, and
"cp" does not even include the '@' ! Even more confusing is the fact that
"cp aa@bb cc" matches (which I expected), but $+ = "cp " !!!

Could anyone help? I want only "cp\s<anything>@<anything>\s<anything>" to
match.

Thanks in advance

O.S.

news@genrad.UUCP (Network News) (05/28/90)

along the line (TAURUS.BITNET), so I have to resort to posting.
From: rep@genrad.com (Pete Peterson)
Path: rep

In article <1336@taurus.BITNET> <shani%math.tau.ac.il@CUNYVM.CUNY.EDU> writes:

>When I use the matching operator on something like
>
>"cp" =~ /cp\s*@*\s*/
>
>it matches, and I think it shouldn't because as far as I understand,
>only expressions of the form "cp\s*@*\s*" (that is -
>cp\s<anything>@<anything>\s<anything> ) should have matched the pattern, and
>"cp" does not even include the '@' ! Even more confusing is the fact that
>"cp aa@bb cc" matches (which I expected), but $+ = "cp " !!!
>
I think that your only problem is that you're confusing "*" in regexps with
"*" in shell syntax.  In regular expressions, RE* means ZERO OR MORE
occurrences of the regexp RE.  To indicate "anything" corresponding to
"*" in shell syntax, you need ".*" where "." is a regexp which matches
"anything" and "*" means zero or more occurrences.  In your example
(cp aa@bb cc), "cp " was matched by "cp\s*" AND also by "cp\s*@*\s*" which
couldn't match any longer string because there was nothing to match the
"a".
>
>Could anyone help? I want only "cp\s<anything>@<anything>\s<anything>" to
>match.
>
I think that what you really want is "cp\s.*@.*\s.*" wherein "cp" matches
"cp" each "\s" matches some whitespace, "@" matches "@" and each ".*"
matches an arbitrary string of characters.

Let me know if this isn't clear and I'll try again.

	pete peterson
	rep@genrad.com
	{decvax,linus,wjh12,mit-eddie,masscomp}!genrad!rep

worley@compass.com (Dale Worley) (05/29/90)

   From: shani@TAURUS.BITNET

   Could anyone help? I want only "cp\s<anything>@<anything>\s<anything>" to
   match.

Use "cp" =~ /cp\s.*@.*\s.*/

Otherwise, the *'s modify the \s's and @ that preceeds them.

Dale Worley		Compass, Inc.			worley@compass.com
--
Just say N O!
          2