Help with substitute in perl.

Discussion in 'Perl Misc' started by Eric.Medlin@gmail.com, May 25, 2005.

  1. Guest

    For the string abc-12-asdfasdf.inc I want to get abc-12-. I can do the
    opposite (ie. get asdfasdf.inc) with $name ~= s/.*-.*-//g, but how do I
    replace not .*-.*- with blank. Thanks.
    , May 25, 2005
    #1
    1. Advertising

  2. wrote:
    > For the string abc-12-asdfasdf.inc I want to get abc-12-. I can do
    > the opposite (ie. get asdfasdf.inc) with $name ~= s/.*-.*-//g, but
    > how do I replace not .*-.*- with blank. Thanks.


    E.g.
    use warnings; use strict;
    my $name = 'abc-12-asdfasdf.inc';
    substr($name, 0, 7, '');
    print $name;
    This code does exactly what you ask for in your example.

    Now, if this is not what you meant, then maybe you need to explain the
    general principle behind what to delete and what to return. One example is
    certainly not enough to deduce a generic pattern.

    jue
    Jürgen Exner, May 25, 2005
    #2
    1. Advertising

  3. Anno Siegel Guest

    Jim Gibson <> wrote in comp.lang.perl.misc:
    > In article <>,
    > <> wrote:
    >
    > > For the string abc-12-asdfasdf.inc I want to get abc-12-. I can do the
    > > opposite (ie. get asdfasdf.inc) with $name ~= s/.*-.*-//g, but how do I
    > > replace not .*-.*- with blank. Thanks.

    >
    > One way:
    >
    > s/[^-]*$//


    Another:

    ( $_) = /(.*-.*-)/;

    Anno
    Anno Siegel, May 26, 2005
    #3
  4. Joe Smith Guest

    wrote:
    > For the string abc-12-asdfasdf.inc I want to get abc-12-. I can do the
    > opposite (ie. get asdfasdf.inc) with $name ~= s/.*-.*-//g, but how do I
    > replace not .*-.*- with blank. Thanks.


    Why don't you simply print out the stuff that has been eliminated?

    if (s/(.*-.*-)//) { print "Removed '$1' from the string\n"; }

    or

    if (my($first,$second) = $string =~ /(.*-.*-)(.*)/) {
    print "first = '$first' and second = '$second' in '$string'\n";
    }

    -Joe
    Joe Smith, May 27, 2005
    #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. Matalote
    Replies:
    2
    Views:
    440
    Matalote
    Jan 26, 2004
  2. Replies:
    1
    Views:
    543
    Jürgen Exner
    Mar 7, 2004
  3. Richard Lee
    Replies:
    5
    Views:
    14,330
    Joe Smith
    Jul 17, 2007
  4. kevin kitenik
    Replies:
    9
    Views:
    188
    Uri Guttman
    Dec 18, 2008
  5. Luc Leemans
    Replies:
    5
    Views:
    141
    C.DeRykus
    Jun 21, 2009
Loading...

Share This Page