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 question

Help for those learning Tcl or writing their own scripts.
Post Reply
g
garung
Voice
Posts: 9
Joined: Wed Oct 26, 2005 4:11 pm

regexp question

Post by garung »

Can someone help me how to skip word like I'll . it's , she's ... in $text? thanks

while {[regexp -indices -start $pos {[^[:punct:]\s]+} $text match]} {
lappend words $match
lassign $match -> pos
incr pos
}
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

<speechles> .tcl set a "I'll . it's , she's ..."
<bot> Tcl: I'll . it's , she's ...
<speechles> .tcl set b [string map {"." "" "?" "" "," ""} $a]
<bot> Tcl: I'll  it's  she's
<speechles> .tcl set c [join [split $b]]
<bot> Tcl: I'll it's she's
Like that? Part c, the join and split is there to eliminate over spaced fields and return to the string to nomal single spaces.
g
garung
Voice
Posts: 9
Joined: Wed Oct 26, 2005 4:11 pm

Post by garung »

speechless, it's tnot that, i mean skip these words(he's she's it's) so that they dont replace them with asterisks
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

garung wrote:speechless, it's tnot that, i mean skip these words(he's she's it's) so that they dont replace them with asterisks

Code: Select all

<speechles> .tcl set a [asterize "this, is a word. this is a question?"]
<bot> Tcl: ****, ** * ****. **** ** * ********?
<speechles> .tcl set a [asterize "he's got this. she's got that. you've got those."]
<bot> Tcl: **'* *** ****. ***'* *** ****. ***'** *** *****.

Code: Select all

proc asterize {text} {
   set asterisktext ""
   foreach word [split $text] {
      set asteriskword ""
      foreach char [split $word ""] {
         if {[string match *$char* "`~!@#$%^&()\[\]\{\}:;'"<>\?,."]} {
            append asteriskword $char
         } else {
            append asteriskword "*"
         }
      }
      append asterisktext "$asteriskword "
   }
   return [string trim $asterisktext]
}
You mean like this?
g
garung
Voice
Posts: 9
Joined: Wed Oct 26, 2005 4:11 pm

Post by garung »

You're so cool! thanks speechles :)
Post Reply