home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 4936 < prev    next >
Encoding:
Text File  |  1992-07-24  |  1.9 KB  |  52 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!darwin.sura.net!wupost!news.utdallas.edu!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Repeat string matching/substituting ?
  5. Message-ID: <1992Jul24.131118.7748@news.eng.convex.com>
  6. Originator: tchrist@pixel.convex.com
  7. Sender: usenet@news.eng.convex.com (news access account)
  8. Nntp-Posting-Host: pixel.convex.com
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. Organization: CONVEX Realtime Development, Colorado Springs, CO
  11. References: <TSHIELDS.92Jul23160911@woodie.oracle.com>
  12. Distribution: comp
  13. Date: Fri, 24 Jul 1992 13:11:18 GMT
  14. X-Disclaimer: This message was written by a user at CONVEX Computer
  15.               Corp. The opinions expressed are those of the user and
  16.               not necessarily those of CONVEX.
  17. Lines: 33
  18.  
  19. From the keyboard of tshields@oracle.com (Thomas A. Shields Jr.):
  20. :I need to surround each occurrence of a string within a line with
  21.  
  22. Blessed (and few :-) are they who spell occurrence right. :-)
  23.  
  24. :begin and end tags, i.e.  "the place the fox went is the chickencoop"
  25. :with keyword "the" needs to become "<begin>the<end> place
  26. :<begin>the<end> fox went is <begin>the<end> chickencoop".
  27. :
  28. :Here's what I tried:
  29. :
  30. :#!/usr/bin/perl -p
  31. :while (/the/g) {
  32. :  $_ = $` . "<begin>" . $& . "<end>" . $';
  33. :}
  34. :
  35. :but this fails unpredictably, probably because //g doesn't like the
  36. :fact that $_ is changing on it.  Any insight into how //g works
  37. :internally, or a better way to accomplish this in general would be
  38. :much appreciated.  Thanks!
  39.  
  40. I think it would be better to put the /g on an s/// expr in this case:
  41.  
  42.     perl -pe 's/\bthe\b/<begin>the<end>/g'
  43.  
  44. --tom
  45.  
  46.  
  47. -- 
  48.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  49.     If you consistently take an antagonistic approach, however, people are
  50.     going to start thinking you're from New York.   :-)
  51.             --Larry Wall to Dan Bernstein in <10187@jpl-devvax.JPL.NASA.GOV>
  52.