Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wildcards and substuting text

Status
Not open for further replies.

KingofSnake

Programmer
Jul 25, 2000
73
US
I'm starting up in perl and, doing the poor student thing, went to Books A Million and glanced over some perl books to try to find out what I needed to do. But I just got more confused then I already was.

What I want to do is to replace all text before a certain character with nothing, and replace all text after a certain charater with nothing. For example:

texttext##special##texttext

I would want to get rid of everything before the first ## and everything after the second ##, creating

##special##

How would I do that?
KingOfSnake - The only sports drink with ice crystals
(and marshmellos!)
 
there's prob a single command way to do this (cue goBoating with a more elegant solution as soon as I've finished :))
[tt]
$spec_chars = '##';
$_ = "texttext##special##texttext";
/$spec_chars/; # find the first one
$_ = $'; # only keep stuff after match
/$spec_chars/; # find the second one
$_ = $`; # only keep stuff before match
$_ = "$spec_chars$_$spec_chars"; # restore special characters
[/tt]
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
This might work too:
$string = "text##special##text";
$specchars = "##";
$string =~ s/(.*)($specchars(.*)$specchars)(.*)/$2/; //Daniel
 
($string = "text##special##text") =~ s/.*##(.*?)##.*/$1/g;
 
Shouldn't you have the ? added to the first and last .* too, to prevent them from matching any # found? i.e.:
Code:
s/.*?##(.*?)##.*?/$1/g;
What should happen if you have one of these cases?

sometext##string1##moretext##string2
sometext##string1##sometext##string2##sometext

Believe me, I've run into cases like that, since I have an HTML preprocessor routine that uses ##...## as a preprocessor tag. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top