newbie regxp question

Discussion in 'Perl Misc' started by Jesper, Sep 30, 2003.

  1. Jesper

    Jesper Guest

    Hi
    I'm trying to use regxp instead of split but I'm having some newbie troubles
    with it. What I really want to accomplish is cutting the string:

    "hello/world/I/am/a/newbie"

    into:

    "hello" and "world/I/am/a/newbie"

    The problem is that the number of backslashes in the string varies from 3 to
    ? so I can't really make a regxp that does
    m/(.*\/)(.*\/)(.*\/)(.*\/)(.*\/)/. Could anyone please help me ?

    Regards, jesper
     
    Jesper, Sep 30, 2003
    #1
    1. Advertising

  2. "Jesper" <> writes:

    > Hi
    > I'm trying to use regxp instead of split but I'm having some newbie troubles
    > with it. What I really want to accomplish is cutting the string:
    >
    > "hello/world/I/am/a/newbie"
    >
    > into:
    >
    > "hello" and "world/I/am/a/newbie"
    >
    > The problem is that the number of backslashes in the string varies from 3 to
    > ? so I can't really make a regxp that does
    > m/(.*\/)(.*\/)(.*\/)(.*\/)(.*\/)/. Could anyone please help me ?


    Well, from your description, it seems like you want the first "word"
    before a backslash, and then everything else. How about...

    m!(\w+)?/(.*)!


    Ryan
    --
    perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
    $;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
    =shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
    +shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'
     
    Ryan Shondell, Sep 30, 2003
    #2
    1. Advertising

  3. Jesper <> wrote:

    > I'm trying to use regxp instead of split but I'm having some
    > newbie troubles with it. What I really want to accomplish is
    > cutting the string:
    >
    > "hello/world/I/am/a/newbie"
    >
    > into:
    >
    > "hello" and "world/I/am/a/newbie"
    >
    > The problem is that the number of backslashes in the string varies
    > from 3 to ? so I can't really make a regxp that does
    > m/(.*\/)(.*\/)(.*\/)(.*\/)(.*\/)/. Could anyone please help me ?


    perldoc -f split

    Just split() it into two pieces:

    my $string = "hello/world/I/am/a/newbie";
    my ($first, $rest) = split '/', $string, 2;
    print "$first, $rest";

    --
    David Wall
     
    David K. Wall, Sep 30, 2003
    #3
  4. Jesper wrote:
    > I'm trying to use regxp instead of split


    That sentence doesn't make sense. The first argument to split _is_ a regular
    expression, so you must use a regexp in order to use split.
    There is no "instead of".

    > but I'm having some newbie
    > troubles with it. What I really want to accomplish is cutting the
    > string:
    >
    > "hello/world/I/am/a/newbie"
    >
    > into:
    >
    > "hello" and "world/I/am/a/newbie"


    Then, what is wrong with
    ($hello, $rest) = split (/\//, "hello/world/I/am/a/newbie", 2);

    jue
     
    Jürgen Exner, Sep 30, 2003
    #4
  5. Jesper

    Jesper Guest

    Hi Ryan
    That works like a charm - thx!
    2 minuttes after I posted I got the following idea

    m/([a-zA-Z0-9]*)\/(.*)/

    What is the difference between m// and m!! ?

    Regards, jesper


    "Ryan Shondell" <-state.edu> wrote in message
    news:-state.edu...
    > "Jesper" <> writes:
    >
    > > Hi
    > > I'm trying to use regxp instead of split but I'm having some newbie

    troubles
    > > with it. What I really want to accomplish is cutting the string:
    > >
    > > "hello/world/I/am/a/newbie"
    > >
    > > into:
    > >
    > > "hello" and "world/I/am/a/newbie"
    > >
    > > The problem is that the number of backslashes in the string varies from

    3 to
    > > ? so I can't really make a regxp that does
    > > m/(.*\/)(.*\/)(.*\/)(.*\/)(.*\/)/. Could anyone please help me ?

    >
    > Well, from your description, it seems like you want the first "word"
    > before a backslash, and then everything else. How about...
    >
    > m!(\w+)?/(.*)!
    >
    >
    > Ryan
    > --
    > perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
    > $;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
    > =shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
    > +shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'
     
    Jesper, Sep 30, 2003
    #5
  6. Jesper

    Jesper Guest

    Ok, fair enough - I just relate the =~ operator to regxp (newbie :)).

    Regards, jesper

    "Jürgen Exner" <> wrote in message
    news:Qijeb.31168$...
    > Jesper wrote:
    > > I'm trying to use regxp instead of split

    >
    > That sentence doesn't make sense. The first argument to split _is_ a

    regular
    > expression, so you must use a regexp in order to use split.
    > There is no "instead of".
    >
    > > but I'm having some newbie
    > > troubles with it. What I really want to accomplish is cutting the
    > > string:
    > >
    > > "hello/world/I/am/a/newbie"
    > >
    > > into:
    > >
    > > "hello" and "world/I/am/a/newbie"

    >
    > Then, what is wrong with
    > ($hello, $rest) = split (/\//, "hello/world/I/am/a/newbie", 2);
    >
    > jue
    >
    >
     
    Jesper, Sep 30, 2003
    #6
  7. Jesper wrote:
    >
    > Ok, fair enough - I just relate the =~ operator to regxp (newbie :)).


    The binding operators (=~ and !~) are not strictly related to regular
    expressions, they can also be used with tr/// which does not use regular
    expressions at all.


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Sep 30, 2003
    #7
  8. Jesper wrote:
    > Ok, fair enough - I just relate the =~ operator to regxp (newbie :)).


    Hmmm, well, aehh, but the binding operator =~ doesn't have anything to do
    with REs at all.
    Of the top of my head there are only three operators/functions which use
    REs:
    - split
    - s
    - m
    I'm sure someone will jump in if I forgot one.

    Note: glob() and its relatives don't use REs but shell wildcards.

    jue
     
    Jürgen Exner, Sep 30, 2003
    #8
  9. Jesper wrote:
    > 2 minuttes after I posted I got the following idea
    >
    > m/([a-zA-Z0-9]*)\/(.*)/
    >
    > What is the difference between m// and m!! ?


    If you use the slash then you can omit the "m".
    Details see the man page for "m" ("perldoc -f m") which will point you to
    the the perlop man page: "perldoc perlop"

    jue
     
    Jürgen Exner, Sep 30, 2003
    #9
    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. brendan
    Replies:
    2
    Views:
    113
    brendan
    Aug 28, 2003
  2. Jerry C.
    Replies:
    8
    Views:
    250
    Uri Guttman
    Nov 23, 2003
  3. Kruno Saho
    Replies:
    0
    Views:
    146
    Kruno Saho
    Apr 7, 2013
  4. Dave Angel
    Replies:
    0
    Views:
    127
    Dave Angel
    Apr 7, 2013
  5. rusi
    Replies:
    0
    Views:
    114
Loading...

Share This Page