Repeat string matching/substituting ? 
Author Message
 Repeat string matching/substituting ?

I am relatively new to Perl, and I am trying to do what seems to be a
fairly simple operation, without much success.

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>" . $';

Quote:
}

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!

-ts-
--

Oracle Corporation, Box 659412, Redwood Shores, CA, 94065   =What .sig virus?=



Tue, 10 Jan 1995 08:09:11 GMT  
 Repeat string matching/substituting ?

:I need to surround each occurrence of a string within a line with

Blessed (and few :-) are they who spell occurrence right. :-)

: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!

I think it would be better to put the /g on an s/// expr in this case:

    perl -pe 's/\bthe\b/<begin>the<end>/g'

--tom

--

    If you consistently take an antagonistic approach, however, people are
    going to start thinking you're from New York.   :-)



Tue, 10 Jan 1995 21:11:18 GMT  
 Repeat string matching/substituting ?

Quote:
>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



Tue, 10 Jan 1995 23:12:15 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Pattern Match - substitute a string after the match

2. split and substitute, substitute, substitute

3. Repeated regex matching.

4. Repeated regex matching.

5. help with pattern match (repeated patterns)

6. Matching a repeated character class series?

7. repeated string

8. RFI: Counting repeated substrings within a string

9. substituting for matches not inside quotes?

10. substitute first match?

11. Substituting the first N matches

12. RegExp: Matching and substituting a part of it

 

 
Powered by phpBB® Forum Software