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

  1. Path: sparky!uunet!oracle!unrepliable!bounce
  2. Newsgroups: comp.lang.perl
  3. From: tshields@oracle.com (Thomas A. Shields Jr.)
  4. Subject: Repeat string matching/substituting ?
  5. Message-ID: <TSHIELDS.92Jul23160911@woodie.oracle.com>
  6. Sender: usenet@oracle.us.oracle.com (Oracle News Poster)
  7. Nntp-Posting-Host: woodie.us.oracle.com
  8. Organization: Oracle Corporation
  9. Distribution: comp
  10. Date: Fri, 24 Jul 1992 00:09:11 GMT
  11. X-Disclaimer: This message was written by an unauthenticated user
  12.               at Oracle Corporation.  The opinions expressed are those
  13.               of the user and not necessarily those of Oracle.
  14. Lines: 24
  15.  
  16. I am relatively new to Perl, and I am trying to do what seems to be a
  17. fairly simple operation, without much success.
  18.  
  19. I need to surround each occurrence of a string within a line with
  20. begin and end tags, i.e.  "the place the fox went is the chickencoop"
  21. with keyword "the" needs to become "<begin>the<end> place
  22. <begin>the<end> fox went is <begin>the<end> chickencoop".
  23.  
  24. Here's what I tried:
  25.  
  26. #!/usr/bin/perl -p
  27. while (/the/g) {
  28.   $_ = $` . "<begin>" . $& . "<end>" . $';
  29. }
  30.  
  31. but this fails unpredictably, probably because //g doesn't like the
  32. fact that $_ is changing on it.  Any insight into how //g works
  33. internally, or a better way to accomplish this in general would be
  34. much appreciated.  Thanks!
  35.  
  36. -ts-
  37. --
  38. Tom Shields, Multimedia Developer      (415) 506-6182      tshields@oracle.com
  39. Oracle Corporation, Box 659412, Redwood Shores, CA, 94065   =What .sig virus?=
  40.