Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie: Regular expresion

Reply
Thread Tools

Newbie: Regular expresion

 
 
Jose Luis
Guest
Posts: n/a
 
      09-18-2009
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

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-18-2009
Jose Luis <> 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
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      09-18-2009
Jose Luis <> 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


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-18-2009
Peter Makholm <> wrote:
>Jose Luis <> 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
 
Reply With Quote
 
Steve C
Guest
Posts: n/a
 
      09-18-2009
Jose Luis wrote:
> 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
>
>


Use 'sed mode':

echo "one;two;three;four..."|perl -pe 's/;.*//'
one

autosplit lets you pick other than first element:

echo "one;two;three;four..."|perl -lanF';' -e'print $F[1]'
two

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expresion, search replace ask8y@yahoo.com Perl Misc 1 02-19-2013 04:06 AM
Regular Expresion Error? Filipe Ruby 4 10-05-2007 01:26 AM
Regular Expresion Needed Matt White Ruby 2 06-19-2007 07:36 PM
Deriving boolean expresion from truth table dayzman@hotmail.com C++ 0 05-04-2005 04:02 AM
Why C++ has ambiguity between expresion statments and declaration statments? Andy C++ 5 01-23-2005 04:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57