This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

regexp/string match badword

Old posts that have not been replied to for several years.
Locked
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

regexp/string match badword

Post by Dedan »

surely you will suggest i "bind' each badword to a proc,
but this is an all-in-one script ... it looks for
codes/caps/badwords and repeats.

i want to set the badwords as a list, and match them againist text.
i also will be using 2 different lists.

set badwords {
"test"
"t e s t"
"*more*"
}


PROBLEM:

If i use:

Code: Select all

    foreach w $badwords {
      if {[string match -nocase "$w" "$strip"]} {

it will not work because:
string does not match the pattern
the entire string had to match the entire pattern, character for character.

If i use:

Code: Select all

    foreach w $badwords {
      if {[string match -nocase "*$w*" "$strip"]} {

it will not work because: it will kick on "testing" ... i only want it to kick on "test"


If i use:

Code: Select all

    foreach w $badwords {
      if {[string match -nocase "* $w *" "$strip"]} {

it will not work if there is only one word typed or if there is no space after the $w



if i split the string to a list and list search,
entrys like "t e s t" can not be matched.

i have tried some regexp, but it will kick on "testing" ... i only want it to kick on "test"


thanks for any help given
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

how about 'string match -nocase "* $w *" " $strip "'?
Have you ever read "The Manual"?
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

If i use:
Code:

foreach w $badwords {
if {[string match -nocase "* $w *" "$strip"]} {



it will not work if there is only one word typed or if there is no space after the $w
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Dedan wrote:If i use:
Code:

foreach w $badwords {
if {[string match -nocase "* $w *" "$strip"]} {



it will not work if there is only one word typed or if there is no space after the $w
...that's why I suggested the additional spaces around $strip
user wrote:how about 'string match -nocase "* $w *" "<SPACE>$strip<SPACE>"'?
Have you ever read "The Manual"?
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

doh
I once was an intelligent young man, now i am old and i can not remember who i was.
U
Unknown1
Voice
Posts: 11
Joined: Fri Jun 11, 2004 5:40 pm

Post by Unknown1 »

I'm having the same problem.. And I came to the same conclusing to just add a space on the front and back of the string.. But there is still a problem...

If the string has any non alpha chars in it, it will still miss the badword.

example:
" This is a test badword. "

It will still miss the badword because " badword. " doesn't match " badword "..

I see a way to strip all color codes out of a string.. Is there a way to strip all non alpha chars?

Or is there a better solution?
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

regsub -all -nocase {[^a-z]+} $line " " line
EDIT: I added a "+" to make it look a bit less paced out ;)
Last edited by user on Thu Jul 01, 2004 5:17 pm, edited 2 times in total.
Have you ever read "The Manual"?
U
Unknown1
Voice
Posts: 11
Joined: Fri Jun 11, 2004 5:40 pm

Post by Unknown1 »

Hmm, as you see I'm new to tcl.. But that looks like a perfect solution.. Trying now.. Thanks..
m
mm
Halfop
Posts: 78
Joined: Thu Jul 01, 2004 10:24 pm

Post by mm »

Hi, when i try as "user" has suggested -> how about 'string match -nocase "* $w *" " $strip "'?

foreach w $bad_words {
if {['string match -nocase "* $w *" " $text "']} {

i get the error
Tcl error [protect:channel]: extra characters after close-quote


Dedan/Unknown1 can i have the working script please?


Thanks
MM
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

Code: Select all

if {['string match -nocase "* $w *" " $text "']} {
there is a couple of ' that's not supposed to be there.

Code: Select all

if {[string match -nocase "* $w *" " $text "]} {
Elen sila lúmenn' omentielvo
m
mm
Halfop
Posts: 78
Joined: Thu Jul 01, 2004 10:24 pm

Post by mm »

Thank you so much.
MM
Locked