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 Problem.. SOS

Help for those learning Tcl or writing their own scripts.
Post Reply
a
angelbroz
Voice
Posts: 1
Joined: Sun Dec 16, 2007 4:51 pm

Regexp Problem.. SOS

Post by angelbroz »

Hi there i'm making a script

I'd like to get somes values from a webpage

>Groundshaker</A> (exori mas) </TD><TD>Attack </TD><TD>Instant</TD><TD>33</TD>.......

i have something

Code: Select all


proc spell {nick uhost hand chan text} {

#set text [string totitle $text]

set query "http://www.tibia.com/library/?subtopic=spells"
set http [::http::geturl $query]
set html [::http::data $http]


set k ">(.*?)</A> \($text\)</TD><TD>(.*?) </TD><TD>(.*?)</TD><TD>(.*?)</TD><TD>(.*?)</TD><TD>(.*?)</TD><TD>(.*?)</TD></TR>"

regexp $k $html - spell cat type lvl mana price prem

  putserv "privmsg $chan :$spell / Formula: $text / Category: $cat / Type: $type / Lvl: $lvl / Mana: $mana / Price: $price / Premium: $prem"

}

but dont works :(

[14:44] <~angelbroz> !spell exori has

Tcl error [spell]: couldn't compile regular expression pattern: parentheses () not balanced

plx help
User avatar
Alchera
Revered One
Posts: 3344
Joined: Mon Aug 11, 2003 12:42 pm
Location: Ballarat Victoria, Australia
Contact:

Post by Alchera »

A little "light" reading is in order: regexp
Add [SOLVED] to the thread title if your issue has been.
Search | FAQ | RTM
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Code: Select all

set timeout "60000"

set text [split [string tolower $text]]

set query "http://www.tibia.com/library/?subtopic=spells"

# Check if we even get the webpage?
catch {set page [::http::geturl $query -timeout $timeout]} error
	if {[string match -nocase "*couldn't open socket*" $error]} {
		puthelp "PRIVMSG $chan :Error: couldn't connect..Try again later."
		return
	}
	if { [::http::status $page] == "timeout" } {
		puthelp "PRIVMSG $chan :Error: Connection to timed out..Try again later."
		return
	}

set html [::http::data $page]

# regexp won't expand $vars, so do it here first:
set regex "HREF=\"http://www.tibia.com/library/?subtopic=spells&spell=.*?\">(.*?)</A> \($text\)</TD><TD>(.*?)</TD><TD>(.*?)</TD><TD>(.*?)</TD><TD>(.*?)</TD><TD>(.*?)</TD><TD>(.*?)</TD></TR><TR BGCOLOR=.*?<TD>"


if {[regexp $regex $html match spell cat type lvl mana price prem]} {
        puthelp "PRIVMSG $chan :$spell / Formula: $text / Category: $cat / Type: $type / Lvl: $lvl / Mana: $mana / Price: $price / Premium: $prem"
} else {
         puthelp "PRIVMSG $chan :Can't get data..."
}
Thats the jist of it. Do more error checking, use a var to expand input vars for regexp, and use PRIVMSG (uppercase) as I do believe it's case sensitive (might be wrong on that, I've always used uppercase.).

The locations of my subexpressions (.*?) might not be completely what you wanted, I just grabbed everything with regexp, edit to suit..
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Actually, since it doesn't look like that webpage is gonna change much at all, why not just grab a copy and store it all in an array after reading it from disk (after cleaning up and saving it to disk), then you don't have to wait for it to grab the webpage everytime there's a query.. Arrays can be fun! :)

My earth calendar script does that, so does the tvrage script. They pull in fresh data once in a while just in case there's been changes..
Post Reply