For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2005 > Help with substitute in perl.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help with substitute in perl.
Eric.Medlin@gmail.com

2005-05-25, 8:56 pm

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ürgen Exner

2005-05-25, 8:56 pm

Eric.Medlin@gmail.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.


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


Jim Gibson

2005-05-25, 8:56 pm

In article <1117055270.506381.141670@g14g2000cwa.googlegroups.com>,
<Eric.Medlin@gmail.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/[^-]*$//


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Anno Siegel

2005-05-25, 8:56 pm

Jim Gibson <jgibson@mail.arc.nasa.gov> wrote in comp.lang.perl.misc:
> In article <1117055270.506381.141670@g14g2000cwa.googlegroups.com>,
> <Eric.Medlin@gmail.com> wrote:
>
>
> One way:
>
> s/[^-]*$//


Another:

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

Anno
Joe Smith

2005-05-27, 4:00 am

Eric.Medlin@gmail.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.


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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com