From: Jose Luis on
Hi,

Given the string "one;two;three;four...", is there a easy way to
print "one":


$ echo "one;two;three;four..."|perl -e 'while(<>){$_ =~ /^(.*)(\;)(.*)
$/ && print $1}'
one;two;three



Thanks in advance,
Jose Luis

From: J�rgen Exner on
Jose Luis <jose.luis.fdez.diaz(a)gmail.com> wrote:
>Given the string "one;two;three;four...", is there a easy way to
>print "one":

use strict; use warnings;
my $s = "one;two;three;four...";
print ((split(/;/, $s))[0]);

jue
From: Peter Makholm on
Jose Luis <jose.luis.fdez.diaz(a)gmail.com> writes:

> Given the string "one;two;three;four...", is there a easy way to
> print "one":

Not using regular expressions I would use the split function to
archieve this.

> $ echo "one;two;three;four..."|perl -e 'while(<>){$_ =~ /^(.*)(\;)(.*)
> $/ && print $1}'
> one;two;three

But what you are missing is the non-greedy variant of the initial
*. By default quantifiers af greedy, that is that they match as much
as posible. If you instead uses (.*?) it will match as little as
posible.

Another soulution woulb be to replace (.*) with ([^;]*). This matches
as many non-semicolon as possible.

//Makholm


From: J�rgen Exner on
Peter Makholm <peter(a)makholm.net> wrote:
>Jose Luis <jose.luis.fdez.diaz(a)gmail.com> writes:
>
>> Given the string "one;two;three;four...", is there a easy way to
>> print "one":
>
>Not using regular expressions I would use the split function to
>archieve this.

Actually the first argument to split() is a regular expression.
But I do strongly share your sentiment about using split() for this task
instead of capturing RE groups.

jue
From: Sherm Pendley on
Jose Luis <jose.luis.fdez.diaz(a)gmail.com> writes:

> Given the string "one;two;three;four...", is there a easy way to
> print "one":

perl -e "print split(';', 'one;two;three;four')[0]"

That said, is this a "how do I parse a query string" question in disguise?
If so, a better answer is "don't do that." There are a plethora of well-
tested and peer reviewed CPAN modules for that.

sherm--