🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.perl
Article #52177

Re: regexp i zmienna

#52177
From: Szymon Sokół
Date: Wed, 23 Feb 2011 20:37
31 lines
2441 bytes
On Wed, 23 Feb 2011 19:49:18 +0100, Michal Jankowski wrote:

> Szymon Sokół <szymon@bastard.operator.from.hell.pl> writes:
> 
>>> $` $& $'
>> $string =~ /(.*)(wzorek)(.*)/
>> my($pierwsza,$druga,$trzecia) = split /(wzorek)/, $string;
> 
> Spróbujcie:
> ------------------------------------------------------------
> #!/usr/bin/perl -w
> $s = "Ala ma kota, a Ola ma psa";
> 
> $s =~ /ma/; print "($`) ($&) ($')\n";
> $s =~ /(.*)(ma)(.*)/; print "($1) ($2) ($3)\n";

Owszem, jeśli komuś zależy na tym, żeby wyłapać *pierwsze*, a nie *ostatnie* 
wystąpienie wzorca, to należałoby użyć w pierwszym nawiasie .*? zamiast .* -
OTOH, tego założenia nie było.

> ($a,$b,$c) = split /(ma)/, $s; print "($a) ($b) ($c)\n";

...a to działa poprawnie tylko w przypadku, gdy wzorzec się w ogóle nie
powtarza. Chodziło mi raczej o pokazanie, że nie jest prawdą "split mi
zwróci tylko 1 i 3 część", niż sugerowanie rozwiązania opartego o split.

-- 
Szymon Sokół (SS316-RIPE) -- Network Manager                                 B
Computer Center, AGH - University of Science and Technology, Cracow, Poland  O
http://home.agh.edu.pl/szymon/ PGP key id: RSA: 0x2ABE016B, DSS: 0xF9289982  F
Free speech includes the right not to listen, if not interested -- Heinlein  H
Technical Details
Message-ID: <51w43zgb6rgc$.dlg@falcon.sloth.hell.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!.POSTED!dustpuppy.sloth.hell.pl!not-for-mail
References: <ik1miv$t6o$1@node2.news.atman.pl> <5qjf38-hb7.ln1@depesz.com> <19qawculecjw2$.dlg@falcon.sloth.hell.pl> <kjzaahmio29.fsf@ccfs1.fuw.edu.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!.POSTED!dustpuppy.sloth.hell.pl!not-for-mail
From: Szymon Sokół
 <szymon@bastard.operator.from.hell.pl>
Newsgroups: pl.comp.lang.perl
Subject: Re: regexp i zmienna
Date: Wed, 23 Feb 2011 21:37:04 +0100
Organization: Guy Fawkes Society
Lines: 31
Sender: szymon@news.agh.edu.pl
Message-ID: <51w43zgb6rgc$.dlg@falcon.sloth.hell.pl>
References: <ik1miv$t6o$1@node2.news.atman.pl> <5qjf38-hb7.ln1@depesz.com> <19qawculecjw2$.dlg@falcon.sloth.hell.pl> <kjzaahmio29.fsf@ccfs1.fuw.edu.pl>
NNTP-Posting-Host: 89-79-74-119.dynamic.chello.pl
Mime-Version: 1.0
X-Trace: news.agh.edu.pl 1298493739 5324 89.79.74.119 (23 Feb 2011 20:42:19 GMT)
X-Complaints-To: abuse@agh.edu.pl
NNTP-Posting-Date: Wed, 23 Feb 2011 20:42:19 +0000 (UTC)
User-Agent: 40tude_Dialog/2.0.15.1 Hamster/2.1.0.11
X-Face: xic?6f"rD)k-tM.]L]A6l4W7_aGt({}&{D,hiRI.8cPtf,1F.w@Qkl!vr23o1UY-BfXO$p< hk5>ijB?eBy)?!C<@)+%U\&`1X)MJ>.i.qgkY]4mvL62j<|xJRG/5:7:l8q]x;v!)Bz^r/!yG8DxoD -@fnC,g]BAXj
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit