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. Advertisements

  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. Advertisements

  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. Advertisements

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. dpackwood
    Replies:
    3
    Views:
    2,153
  2. Richard Lee
    Replies:
    5
    Views:
    14,599
    Joe Smith
    Jul 17, 2007
  3. kevin kitenik
    Replies:
    9
    Views:
    421
    Uri Guttman
    Dec 18, 2008
  4. Luc Leemans
    Replies:
    5
    Views:
    274
    C.DeRykus
    Jun 21, 2009
  5. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    506
    PerlFAQ Server
    Jan 23, 2011
  6. PerlFAQ Server
    Replies:
    0
    Views:
    987
    PerlFAQ Server
    Feb 3, 2011
  7. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    504
    PerlFAQ Server
    Feb 27, 2011
  8. PerlFAQ Server
    Replies:
    0
    Views:
    949
    PerlFAQ Server
    Apr 4, 2011
Loading...