Help with substitute in perl.

E

Eric.Medlin

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

Jürgen Exner

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
 
A

Anno Siegel

Jim Gibson said:
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
 
J

Joe Smith

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top