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.

HELP! Regexp/Regsub

Help for those learning Tcl or writing their own scripts.
Post Reply
m
maphex
Voice
Posts: 9
Joined: Fri Feb 10, 2012 5:06 pm

HELP! Regexp/Regsub

Post by maphex »

So what im trying to do here is make a trigger that is based on a SQL entry.
So the first !(.*) will be the trigger.
Then I want the next (.*) to be the search term(s).

Everything works fine if the search term is one word but if the term is broken by a space like !searchchan search words here the script fails.

So I need to make it take the second (.*) and extend to the end of the line and save it as a var. Then the regsub will then take it and break it into usable content the rest of the script can use.

Code: Select all

    bind pubm - * bind_search
    proc bind_search {nick uhost handle channel text} { 

      filter_mirc_codes
	set regexp {^!(.*) (.*) *$}
	if {[regexp -nocase -- $regexp $text -> searchchan search]} {
	  
	regsub -all -- {\*} $search % search
	set searchvar [list]
	foreach word [split $search] {
	lappend searchvar db.`searchcolum` like '%[mysqlescape $word]%'"
	}
	
	do sql stuff like getting results 
        where [join $searchvar " AND " ]
	AND SearchChannel LIKE '#%$searchchan%'
	}
}
Added this just in case someone need to know theproc for the filter_mirc_codes

Code: Select all

    proc filter_mirc_codes {} {
      upvar text text
      regsub -all -- "(\002|\017|\026|\037|\003(\[0-9\]\[0-9\]?(,\[0-9\]\[0-9\]?)?)?)"  $text "" text
}
Big thanks in advance! I am open to other options that maybe fast or cleaner to the way I am doing it.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Why not simply use [split $text] on the text.
Then [lindex $text 0] would be the first word and [lrange $text 1 end] would be all the other words, still as a list.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
m
maphex
Voice
Posts: 9
Joined: Fri Feb 10, 2012 5:06 pm

Post by maphex »

Could you provide me with an example? I understand a little better when its laid out and can see how it works? Thanks for the fast response!
N
Nimos
Halfop
Posts: 80
Joined: Sun Apr 20, 2008 9:58 am

Post by Nimos »

Code: Select all

   if {[regexp -nocase -- {^!.* .*$} $text]} { 
     
      set trigger [lindex [split $text] 0]
      set search [lrange [split $text] 1 end]
   
      regsub -all -- {\*} $search % search 
      
      set searchvar [list] 
      foreach word $search { 
         lappend searchvar db.`searchcolum` like '%[mysqlescape $word]%'" 
      } 
    
      do sql stuff like getting results 
        where [join $searchvar " AND " ] 
   AND SearchChannel LIKE '#%$searchchan%' 
   } 
m
maphex
Voice
Posts: 9
Joined: Fri Feb 10, 2012 5:06 pm

Post by maphex »

Thanks for all the help. I got it working with some modifications to the code to not use a pubm and used a regular bind pub and still used the split text that Nimos provided.

I decided against the regexp method because of overlapping binds using pubm unless there is something i missed to stop this
Post Reply