substituting keywords in a string 
Author Message
 substituting keywords in a string

Quote:

>A couple of weeks back I posted this problem and got it solved. However, I
realised there was
>something I overlooked.

>This is the code I'm currently using.

>------------------------------------------------------------

>$string = "Albert Einstein, Isaac Newton, Marie Curie";


> {
> $string =~ s/($_)/<FONT COLOR=DARKRED>$1<\/FONT>/gi;
> }

>print $string;

----------------------------------------------------------------

Try this instead


$string = "To be or not to be, that is the question";


 {
 $string =~ s/\s($_)\s/ <font color=darkred>$1<\/font> /gi;
 }

print $string;
----------------------------------------------------------------

First put a space before and after your <>s so the words don't run together
and then put a \s (space) before and after your search pattern so that it
only finds whole words.



Fri, 20 Jul 2001 03:00:00 GMT  
 substituting keywords in a string
A couple of weeks back I posted this problem and got it solved. However, I realised there was
something I overlooked.

This is the code I'm currently using.


$string = "Albert Einstein, Isaac Newton, Marie Curie";


        {
        $string =~ s/($_)/<FONT COLOR=DARKRED>$1<\/FONT>/gi;
        }

print $string;
----------------------------------------------------------------

The problem arises in this scenario:


$string = "To be or not to be, that is the question";


        {
        $string =~ s/($_)/<font color=darkred>$1<\/font>/gi;
        }

print $string;
----------------------------------------------------------------

The code substitutes "to" and "be" fine, but when it comes to substituting "or", it will also
substitute the "or" in "<font colOR=darkred>", resulting in this:

<font col<font color=darkred>=darkred>

thus messing up my html. The same goes for "font", "color", "dark", "red"

Is there a better way to handle the substituting?

Any help would be appreciated. Thanks.

Jason Q.



Sat, 21 Jul 2001 03:00:00 GMT  
 substituting keywords in a string


Quote:
> The code substitutes "to" and "be" fine, but when it comes to substituting "or", it will also
> substitute the "or" in "<font colOR=darkred>", resulting in this:
> Is there a better way to handle the substituting?

Use \b in your regexp

# perldoc perlre

Martien
--
Martien Verbruggen                  |
Interactive Media Division          |
Commercial Dynamics Pty. Ltd.       | Curiouser and curiouser, said Alice.
NSW, Australia                      |



Sat, 21 Jul 2001 03:00:00 GMT  
 substituting keywords in a string

Quote:



>> The code substitutes "to" and "be" fine, but when it comes to substituting "or", it will also
>> substitute the "or" in "<font colOR=darkred>", resulting in this:

>> Is there a better way to handle the substituting?

>Use \b in your regexp

Like that?

$string =~ s/\b($_)\b/<font color=darkred>$1<\/font>/gi;

if keywords included "font", "color" or "darkred", my problem would still be there, wouldn't it?

Jason Q.

Quote:
># perldoc perlre

>Martien
>--
>Martien Verbruggen                  |
>Interactive Media Division          |
>Commercial Dynamics Pty. Ltd.       | Curiouser and curiouser, said Alice.
>NSW, Australia                      |



Sat, 21 Jul 2001 03:00:00 GMT  
 substituting keywords in a string

: The problem arises in this scenario:

: ---------------------------------------------------------------

: $string = "To be or not to be, that is the question";


:       {
:       $string =~ s/($_)/<font color=darkred>$1<\/font>/gi;
:       }

: print $string;
: ----------------------------------------------------------------

: The code substitutes "to" and "be" fine, but when it comes to substituting "or", it will also
: substitute the "or" in "<font colOR=darkred>", resulting in this:

: <font col<font color=darkred>=darkred>

: thus messing up my html. The same goes for "font", "color", "dark", "red"

: Is there a better way to handle the substituting?

Your problem is that after you do a substitution, you rescan the
(now-transformed) string from the beginning, thus matching text that was
already substituted.

What you need to do is harness the power of regexps to do your
substitutions in parallel rather than in series:


$string = "To be or not to be, that is the question";
$string =~ s/($kwstring)/<font color=darkred>$1<\/font>/gi;
print $string;



Sat, 21 Jul 2001 03:00:00 GMT  
 substituting keywords in a string

[...]

Quote:
> Your problem is that after you do a substitution, you rescan the
> (now-transformed) string from the beginning, thus matching text that was
> already substituted.

> What you need to do is harness the power of regexps to do your
> substitutions in parallel rather than in series:



> $string = "To be or not to be, that is the question";
> $string =~ s/($kwstring)/<font color=darkred>$1<\/font>/gi;
> print $string;

Exactly right. May I suggest a variation? (Thanx Abigail.)


   { local $" = '|';

   }

Safer:

   { local $" = '|';

                 [<font color="darkred">$1</font>]gi;
   }

Andre



Sat, 21 Jul 2001 03:00:00 GMT  
 substituting keywords in a string

: >Use \b in your regexp

: Like that?

: $string =~ s/\b($_)\b/<font color=darkred>$1<\/font>/gi;

   Yes.

: if keywords included "font", "color" or "darkred", my problem would still be there, wouldn't it?

   Yes.

   If
      1) you use the suggestion in one of the other followups
         where you check for _all_ of the words with one s///g

      and

      2) there are no <font> tags in the data you are operating on

   Then

      Your problem is solved.

   If 'font', 'color', etc is already in your data, then you
   still have a (much harder) problem.

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Sat, 21 Jul 2001 03:00:00 GMT  
 substituting keywords in a string


Quote:

>>Use \b in your regexp

> Like that?

> $string =~ s/\b($_)\b/<font color=darkred>$1<\/font>/gi;

Yep.

Quote:
> if keywords included "font", "color" or "darkred", my problem would
> still be there, wouldn't it?

Yep. If your string contains HTML, and your replacement stuff contains
HTML keywords then you have a serious problem. The only way to do it
correctly, that I can think of, is to use HTML::Parser or its ilk to
parse the HTML, and only do substitutions on the text parts.

If you _start_ with a string without HTML, then all you need to do is
do all the substitutions in one go, as ex0plained in another answer to
your question.

HTH,
Martien
--
Martien Verbruggen                  |
Interactive Media Division          | Begin at the beginning and go on till
Commercial Dynamics Pty. Ltd.       | you come to the end; then stop.
NSW, Australia                      |



Sat, 21 Jul 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Substitute scalar for a keyword?

2. split and substitute, substitute, substitute

3. To keyword or not to keyword, that is the question

4. Efficient keyword/value lookups using keyword substrings

5. Matching a string between 2 keywords

6. Search x keywords in a string ?

7. Substituting many strings in one pass over the input

8. Substituting many sparsely distrbuted strings in many files

9. substituting strings in source trees?

10. Pattern Match - substitute a string after the match

11. Repeat string matching/substituting ?

12. substituting string of text

 

 
Powered by phpBB® Forum Software