home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!decwrl!sgi!wdl1!wdl39!mab
- From: mab@wdl39.wdl.loral.com (Mark A Biggar)
- Newsgroups: comp.lang.perl
- Subject: Re: Repeat string matching/substituting ?
- Message-ID: <1992Jul24.151215.15213@wdl.loral.com>
- Date: 24 Jul 92 15:12:15 GMT
- References: <TSHIELDS.92Jul23160911@woodie.oracle.com>
- Sender: news@wdl.loral.com
- Distribution: comp
- Organization: Loral Western Development Labs
- Lines: 23
-
- In article <TSHIELDS.92Jul23160911@woodie.oracle.com> tshields@oracle.com (Thomas A. Shields Jr.) writes:
- >I need to surround each occurrence of a string within a line with
- >begin and end tags, i.e. "the place the fox went is the chickencoop"
- >with keyword "the" needs to become "<begin>the<end> place
- ><begin>the<end> fox went is <begin>the<end> chickencoop".
- >Here's what I tried:
- >#!/usr/bin/perl -p
- >while (/the/g) {
- > $_ = $` . "<begin>" . $& . "<end>" . $';
- >}
- >but this fails unpredictably, probably because //g doesn't like the
- >fact that $_ is changing on it. Any insight into how //g works
- >internally, or a better way to accomplish this in general would be
- >much appreciated. Thanks!
-
- Boy that's the hard way. What's wrong with:
-
- s/the/<begin>the<end>/g;
-
- --
- Perl's Maternal Uncle
- Mark Biggar
- mab@wdl1.wdl.loral.com
-