From: Jose Luis on 18 Sep 2009 05:17 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 18 Sep 2009 07:25 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 18 Sep 2009 07:41 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 18 Sep 2009 08:19 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 18 Sep 2009 09:42 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--
|
Next
|
Last
Pages: 1 2 Prev: SIGUSR1 ignored during poll() if sleep() and warn() called? Next: decimal round off issue |