develooper Front page | perl.beginners | Postings from March 2002

Re: Vexing regex question--storing multiple hits for later use

Thread Previous | Thread Next
From:
Jenda Krynicky
Date:
March 6, 2002 02:31
Subject:
Re: Vexing regex question--storing multiple hits for later use
Message ID:
3C85FE1A.18907.4FEC921@localhost
From: "Daniel J. Tomso" <tomso@niehs.nih.gov>
> I'm matching a pattern against a long string, and I want to examine
> each occurrence of the match.  I know that $& stores the match, but is
> there a way to store EVERY match in the string, and then look at them?

Dont use $&, it'll slow down ALL regexps even if you use it once!

> Example:
> 
> $str = "This is a very long string, isn't it?"
> 
> If I want to match against something like /i[st]/ to pick up all the
> 'is' and 'it' strings, then see what I've got (i.e. each instance),
> can I do it?

Either

	@matches = ($str =~ /(i[st])/g);

or

	while ($str =~ /(i[st])/g) {
		my $match = $1;
		...
	}

Jenda

=========== Jenda@Krynicky.cz == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
					--- me

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About