home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 4940 < prev    next >
Encoding:
Internet Message Format  |  1992-07-24  |  1.2 KB

  1. Path: sparky!uunet!olivea!decwrl!sgi!wdl1!wdl39!mab
  2. From: mab@wdl39.wdl.loral.com (Mark A Biggar)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Repeat string matching/substituting ?
  5. Message-ID: <1992Jul24.151215.15213@wdl.loral.com>
  6. Date: 24 Jul 92 15:12:15 GMT
  7. References: <TSHIELDS.92Jul23160911@woodie.oracle.com>
  8. Sender: news@wdl.loral.com
  9. Distribution: comp
  10. Organization: Loral Western Development Labs
  11. Lines: 23
  12.  
  13. In article <TSHIELDS.92Jul23160911@woodie.oracle.com> tshields@oracle.com (Thomas A. Shields Jr.) writes:
  14. >I need to surround each occurrence of a string within a line with
  15. >begin and end tags, i.e.  "the place the fox went is the chickencoop"
  16. >with keyword "the" needs to become "<begin>the<end> place
  17. ><begin>the<end> fox went is <begin>the<end> chickencoop".
  18. >Here's what I tried:
  19. >#!/usr/bin/perl -p
  20. >while (/the/g) {
  21. >  $_ = $` . "<begin>" . $& . "<end>" . $';
  22. >}
  23. >but this fails unpredictably, probably because //g doesn't like the
  24. >fact that $_ is changing on it.  Any insight into how //g works
  25. >internally, or a better way to accomplish this in general would be
  26. >much appreciated.  Thanks!
  27.  
  28. Boy that's the hard way.  What's wrong with:
  29.  
  30. s/the/<begin>the<end>/g;
  31.  
  32. --
  33. Perl's Maternal Uncle
  34. Mark Biggar
  35. mab@wdl1.wdl.loral.com
  36.