egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[solved] regexp

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Elfriede
Halfop


Joined: 07 Aug 2007
Posts: 67

PostPosted: Mon Nov 08, 2010 4:55 am    Post subject: [solved] regexp Reply with quote

Hi everyone Smile

I need some kind of advice how to do the following:

I'd like to use like:

!regex search phrase -a=we -s=home or
!regex -a=we -s=home search phrase

Therefore i need a regex or an alternative to get it like
$var1 = search phrase
$var2 = home
$var3 = we

It should work in both cases, either the search phrase is at the beginning or at the end of the line. The search phrase can consist of one, two or more words.

I appreciate any kind of help!

thanks


Last edited by Elfriede on Sat Jan 01, 2011 5:24 am; edited 2 times in total
Back to top
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Tue Nov 09, 2010 7:42 pm    Post subject: Reply with quote

You might need to be somewhat clearer regarding your needs in order to elicit a satisfactory response.

In the meantime, if it is any help, it is possible to use a regular expression to search for a word or phrase.

Code:

[23:13] <@arfer> % set phrase "hello there"
[23:13] <@Baal> hello there
[23:16] <@arfer> % return [regexp -nocase -- [subst -nocommands -nobackslashes {(\A|\s)${phrase}(\s|\Z)}] "I said Hello There to start the conversation"]
[23:16] <@Baal> 1


For the most part a string match command would be used for such things but there are subtle differences.

A string match using the pattern "*${phrase}*" would return 1 for the text "I said hello therebye starting the conversation" whereas the above regular expression would return 0.
_________________
I must have had nothing to do
Back to top
View user's profile Send private message
Elfriede
Halfop


Joined: 07 Aug 2007
Posts: 67

PostPosted: Fri Dec 31, 2010 1:55 pm    Post subject: Reply with quote

ok ill try to explain different. My bot can search in a mysql database. therefore ive coded a command !regex, which should support different switches, as easy as possible.

1. !regex search phrase - supporting wildcards like !regex %search%
2. the switches are like -a= and -s=

Examples:
!regex my%search% -a=we
!regex -a=we my%search%
!regex my%search% -a=we -s=we2
!regex -s=we2 -a=we my%search%

Im looking for a way to get the switches and the searchphrase fast and "easy". If possible it shouldnt matter, in which order they are used.

Hope this describes it better and someone can provide me a solution Smile

Happy New Year!
Back to top
View user's profile Send private message
speechles
Revered One


Joined: 26 Aug 2006
Posts: 1398
Location: emerald triangle, california (coastal redwoods)

PostPosted: Fri Dec 31, 2010 6:45 pm    Post subject: Reply with quote

Regex isn't the simplest solution in this case. This is easier than you are making it seem. Use the example below and add as many switches as you would like. Wink

Code:
... add this inside your proc ...
# init search variables, create empty lists
set search [list] ; set search_a [list] ; set search s [list]
# iterate user input, parse out switches
foreach word [split $text] {
   # parse switches with the switch statement. oh the irony ;)
   # [string range $word 3 end] removes the switch itself from
   # the word before adding the rest of the word to those lists.
   switch -glob -- $word {
      "-a=*" { lappend search_a [string range $word 3 end] }
      "-s=*" { lappend search_s [string range $word 3 end] }
      default { lappend search $word }
   }
}
# join the search, search_a, and search_s lists into strings we can use
set search [join $search] ; set search_a [join $search_a] ; set search_s [join $search_s]
# Use these 3 variables in your script now.
# $search = users search terms
# $search_a = all switches starting with -a combined
# $search_s = all switches starting with -s combined


Doing it this way, allows for any combinations and placements as well as stacking switches or search elements. This way is intuitive.

Any terms with known switches are applied to each switches list, which makes it possible to stack switches or search elements like the example below:

-s=foo my%search% -a=we -a=also -a=this -a=too -s=bar

in the above case, this is how the three variables you get would look:
$search = my%search%
$search_a = we also this too
$search_s = foo bar

This should be how you envisioned it.
_________________
speechles' eggdrop tcl archive
Back to top
View user's profile Send private message
Elfriede
Halfop


Joined: 07 Aug 2007
Posts: 67

PostPosted: Sat Jan 01, 2011 5:15 am    Post subject: Reply with quote

Perfect ! Thank you very, very much speechles - exactly how i imagined it Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber