atom feed7 messages in org.freebsd.freebsd-questionsRe: Perl question
FromSent OnAttachments
Christopher RuedOct 2, 2000 11:00 am 
Mark OvensOct 2, 2000 11:15 am 
Mark OvensOct 2, 2000 11:25 am 
Andresen,Jason R.Oct 2, 2000 11:36 am 
Christopher RuedOct 2, 2000 2:09 pm 
Mark OvensOct 2, 2000 3:34 pm 
Dirk MyersOct 2, 2000 7:21 pm 
Subject:Re: Perl question
From:Christopher Rued (c.r@xsb.com)
Date:Oct 2, 2000 2:09:28 pm
List:org.freebsd.freebsd-questions

Andresen,Jason R. writes:

BTW, your RE should have a ``*'' as well:

/x.*?y/

Maybe, it depends on exactly what he was trying to get.

The first 3 character match where x and y are the first and third character respectivly, then x.y is exactly what you want. The smallest set of characters that have x and y as boundry values? Then your x.*?y is correct. The smallest set of characters that have x and y as boundries and have at least one character in between them? x.+?y is needed.

The RE I used was precisely what I wanted: x.y (an `x' followed by exactly one character followed by a `y').

When I run the following:

#!/usr/bin/perl $a = "xayxbyxcyxdy"; @s = $a =~ /x.y/; print "\@s is @s\n";

I get:

@s is 1

So, I seem to be getting the truth value rather than the first match in the string. If, however, I wrap the entire RE in a parentheses (make it a subexpression) like so:

#!/usr/bin/perl $a = "xayxbyxcyxdy"; @s = $a =~ /(x.y)/; print "\@s is @s\n";

I get the results I wanted to begin with:

@s is xay

(I discovered this shortly after I sent the first message about this).

What confuses me is that if I specify the global option, I do not need to use a subexpression. For example, if I run the following code:

#!/usr/bin/perl $a = "xayxbyxcyxdy"; @s = $a =~ /x.y/g; print "\@s is @s\n";

I get:

@s is xay xby xcy xdy

So, this leaves me with a couple of questions, the main one being: Why the different treatment for single matches and global matches?

and a less important one: Why is there no way to have the first match assigned to a scalar, since we can be sure that there will be at most one match returned?

If anyone can explain this, and/or answer the questions posed above, I'd appreciate it.

-Chris

To Unsubscribe: send mail to majo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message