Newbie: Regular expresion

Discussion in 'Perl Misc' started by Jose Luis, Sep 18, 2009.

  1. Jose Luis

    Jose Luis Guest

    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
     
    Jose Luis, Sep 18, 2009
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Sep 18, 2009
    #2
    1. Advertising

  3. 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
     
    Peter Makholm, Sep 18, 2009
    #3
  4. 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
     
    Jürgen Exner, Sep 18, 2009
    #4
  5. Jose Luis

    Steve C Guest

    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
     
    Steve C, Sep 18, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    5
    Views:
    484
    Ron Natalie
    Jan 23, 2005
  2. Replies:
    0
    Views:
    465
  3. Matt White

    Regular Expresion Needed

    Matt White, Jun 19, 2007, in forum: Ruby
    Replies:
    2
    Views:
    101
    Bertram Scharpf
    Jun 19, 2007
  4. Filipe

    Regular Expresion Error?

    Filipe, Sep 29, 2007, in forum: Ruby
    Replies:
    4
    Views:
    121
    Filipe
    Oct 5, 2007
  5. Replies:
    1
    Views:
    169
    Jürgen Exner
    Feb 19, 2013
Loading...

Share This Page