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.

[solved] regexp

Help for those learning Tcl or writing their own scripts.
Post Reply
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

[solved] regexp

Post by Elfriede »

Hi everyone :)

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.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

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: Select all

[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
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Post by Elfriede »

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 :)

Happy New Year!
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

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. ;)

Code: Select all

... 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.
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Post by Elfriede »

Perfect ! Thank you very, very much speechles - exactly how i imagined it :)
Post Reply