Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help with substitute in perl.

Reply
Thread Tools

Help with substitute in perl.

 
 
Eric.Medlin@gmail.com
Guest
Posts: n/a
 
      05-25-2005
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.

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      05-25-2005
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


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      05-25-2005
Jim Gibson <> wrote in comp.lang.perl.misc:
> In article < .com>,
> <> 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
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-27-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
any MCDST exams can substitute 070-270 exam in MCSE? Daud Christian MCSE 30 05-30-2005 06:37 PM
Techie Re: any MCDST exams can substitute 070-270 exam in MCSE? Seelan MCSE 0 05-27-2005 03:25 PM
substitute for nested select query DC Gringo ASP .Net 6 05-03-2005 08:59 PM
Substitute UC with space UC flamencoman@earthlink.net Perl 1 03-07-2004 08:02 PM
3rd party reporting tools to substitute Crystal Matalote ASP .Net 2 01-26-2004 04:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57