thorny little regexp problem 
Author Message
 thorny little regexp problem

Hello,

Anyone figure this out?

It is supposed to go through a long string of text (arg1, arg2) and
look for a match with a small string ($str) . Then it is supposed to
substitute $str with <B>$str</B> (html bold) but ONLY if $str is a
whole word within arg1 or arg 2. (A previous check has made sure that
the first char of $str is the first char of a word).

It works beautifully for $str_bits eq "1", when there is only one
$str, but it makes every instance of $str_bit bold (even when not a
whole word) in the while loop underneath.

What am I too dense to spot??

Thanks,

Nick

_________________________

&make_bold(arg1,arg2);

sub make_bold {
  if ($str_bits eq "1") {
    $_[0] =~ s/$str(\W)/<B>$str<\/B>\1/g;
    $_[1] =~ s/$str(\W)/<B>$str<\/B>\1/g;
  } else {

      $_[0] =~ s/$str_bit(\W)/<B>$str_bit<\/B>\1/g;
      $_[1] =~ s/$str_bit(\W)/<B>$str_bit<\/B>\1/g;
    }
  }

Quote:
}

__________________________

============================
Nick Tonkin

http://www.*-*-*.com/



Tue, 11 Jan 2000 03:00:00 GMT  
 thorny little regexp problem

Quote:

> Hello,

> Anyone figure this out?

> It is supposed to go through a long string of text (arg1, arg2) and
> look for a match with a small string ($str) . Then it is supposed to
> substitute $str with <B>$str</B> (html bold) but ONLY if $str is a
> whole word within arg1 or arg 2. (A previous check has made sure that
> the first char of $str is the first char of a word).

> It works beautifully for $str_bits eq "1", when there is only one
> $str, but it makes every instance of $str_bit bold (even when not a
> whole word) in the while loop underneath.

> What am I too dense to spot??

> Thanks,

> Nick

> _________________________

> &make_bold(arg1,arg2);

> sub make_bold {
>   if ($str_bits eq "1") {
>     $_[0] =~ s/$str(\W)/<B>$str<\/B>\1/g;
>     $_[1] =~ s/$str(\W)/<B>$str<\/B>\1/g;
>   } else {

>       $_[0] =~ s/$str_bit(\W)/<B>$str_bit<\/B>\1/g;
>       $_[1] =~ s/$str_bit(\W)/<B>$str_bit<\/B>\1/g;
>     }
>   }
> }

I'm not sure about the reason for the different behavior between the two
parts, but have you tried using \b (word boundary) around your strings?
This would make your regex lines look like:

$_[0] =~ s/\b$str\b/<B>$str<\/B>/g;

I removed the (\W) and \1 from the string, because I can't see their
purpose, unless this was being used to signify a word break.

Bryan
--
-------------------------------
|  Bryan Hart                
|  Network Products Engineer  
|  Engineering Animation Inc.
|  Phone: (515) 296-5979
|  Fax: (515) 296-7025

|  Web: http://www.eai.com/                          
-------------------------------
"A conclusion is simply the place where you got tired of thinking"



Tue, 11 Jan 2000 03:00:00 GMT  
 thorny little regexp problem

Quote:

> Hello,

> Anyone figure this out?

> It is supposed to go through a long string of text (arg1, arg2) and
> look for a match with a small string ($str) . Then it is supposed to
> substitute $str with <B>$str</B> (html bold) but ONLY if $str is a
> whole word within arg1 or arg 2. (A previous check has made sure that
> the first char of $str is the first char of a word).

> It works beautifully for $str_bits eq "1", when there is only one
> $str, but it makes every instance of $str_bit bold (even when not a
> whole word) in the while loop underneath.

> What am I too dense to spot??

> Thanks,

> Nick

> _________________________

> &make_bold(arg1,arg2);

> sub make_bold {
>   if ($str_bits eq "1") {
>     $_[0] =~ s/$str(\W)/<B>$str<\/B>\1/g;
>     $_[1] =~ s/$str(\W)/<B>$str<\/B>\1/g;
>   } else {

>       $_[0] =~ s/$str_bit(\W)/<B>$str_bit<\/B>\1/g;
>       $_[1] =~ s/$str_bit(\W)/<B>$str_bit<\/B>\1/g;
>     }
>   }
> }

> __________________________

> ============================
> Nick Tonkin

> http://www.web-ignite.com

  Firstly you should be using $1 and not \1, turn on warnings and you
will see. Secondly the above function should fail anyway as you can't

something like:
  /\b$str\b/



string like 'simon told fred that it was simons not freds' I only get
the 'simon' and 'fred' surrounded by <B> <\B>.

What version of perl are you using?

Simon



Tue, 11 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. 2 little problems (regexp and do)

2. regexp: matching at least n chars out of a string of length m

3. Silly little regexp question

4. Little Perl Problem...

5. Nice little problem....

6. A little example of non-orthogonality problem

7. Perl 4.009 has at least one serious problem

8. problem (at least for me) with pack()

9. label-less dump problem

10. Having a little problem with changing multiple lines in an html file

11. Problem: MLDBM makes enormous DB files out of little data

12. A little problem with Perl code

 

 
Powered by phpBB® Forum Software