striped name to variable

Discussion in 'Perl Misc' started by blnukem, Apr 12, 2004.

  1. blnukem

    blnukem Guest

    Hi All

    How do I strip a word out of a phrase and save the striped part as a
    variable example:

    $phrase = "My name is Nick<br>";
    $phrase =~ s/Nick//is;

    Now how do I set a new variable that equals the striped part "Nick"
     
    blnukem, Apr 12, 2004
    #1
    1. Advertising

  2. blnukem

    Sam Holden Guest

    On Mon, 12 Apr 2004 13:35:57 GMT, blnukem <> wrote:
    > Hi All
    >
    > How do I strip a word out of a phrase and save the striped part as a
    > variable example:
    >
    > $phrase = "My name is Nick<br>";
    > $phrase =~ s/Nick//is;
    >
    > Now how do I set a new variable that equals the striped part "Nick"


    $new_variable = 'Nick';

    Or if we pretend Nick isn't a constant string:

    if ($phrase =~ s/(Nick)//is) {
    $new_variable = $1;
    }

    --
    Sam Holden
     
    Sam Holden, Apr 12, 2004
    #2
    1. Advertising

  3. blnukem

    Paul Lalli Guest

    On Mon, 12 Apr 2004, blnukem wrote:

    > Hi All
    >
    > How do I strip a word out of a phrase and save the striped part as a
    > variable example:
    >
    > $phrase = "My name is Nick<br>";
    > $phrase =~ s/Nick//is;
    >
    > Now how do I set a new variable that equals the striped part "Nick"



    Save your matches with parentheses, then access them with the $1, $2, $3,
    .... variables.

    my $name;
    if ($phrase =~ s/(Nick)//i){
    $name = $1;
    }


    run `perldoc perlre` and search for 'capture' for more info.

    On an unrelated note, why are you using the /s modifier in your RegExp?
    It serves no purpose here.

    Paul Lalli
     
    Paul Lalli, Apr 12, 2004
    #3
  4. blnukem

    GreenLight Guest

    "blnukem" <> wrote in message news:<1Xwec.5948$>...
    > Hi All
    >
    > How do I strip a word out of a phrase and save the striped part as a
    > variable example:
    >
    > $phrase = "My name is Nick<br>";
    > $phrase =~ s/Nick//is;
    >
    > Now how do I set a new variable that equals the striped part "Nick"


    my $stripped = ($phrase =~ s/(Nick)//i);

    --
    my real address is perl - at - milbaugh - dot - com
     
    GreenLight, Apr 12, 2004
    #4
    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. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    892
    Roedy Green
    Jan 2, 2006
  2. Bobby Chamness
    Replies:
    2
    Views:
    2,442
    Joe Smith
    Apr 22, 2007
  3. mfglinux
    Replies:
    11
    Views:
    752
    Roberto Bonvallet
    Sep 12, 2007
  4. newwen
    Replies:
    0
    Views:
    322
    newwen
    Feb 17, 2009
  5. David Filmer
    Replies:
    19
    Views:
    281
    Kevin Collins
    May 21, 2004
Loading...

Share This Page