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.

regsub -nocase {} help

Help for those learning Tcl or writing their own scripts.
Post Reply
w
walker
Voice
Posts: 6
Joined: Thu Sep 03, 2009 1:05 am

regsub -nocase {} help

Post by walker »

i have code like this:

Code: Select all

set arg_date 15

regsub -nocase {.*<tr\s+bgcolor=\#0099FF><td\s+align=center><FONT SIZE=\'2\'\s+COLOR=\'\#FFFFFF\'\s+face=\'verdana\'>$arg_date(.+)&periuk=.*} $data {\1} data 
 
putquick "PRIVMSG $chan :$data" 
why it's not working?? i think i wrong the code between \'verdana\'> and (.+)&periuk=. please help me. thank you....
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

not sure what you are trying to do, but I put up this one and also returns $data

Code: Select all

set arg_date 15

set data "<tr bgcolor=#0099FF><td align=center><FONT SIZE='2' COLOR='#FFFFFF' face='verdana'>$arg_date&periuk="
regsub -nocase {.+<tr\s+bgcolor=\#0099FF><td\s+align=center><FONT SIZE=\'2\'\s+COLOR=\'\#FFFFFF\'\s+face=\'verdana\'>.+&periuk=.*} $data {\1} data
 
puts "x $data"
which returned:

Code: Select all

(Tcl) 1 % source regextest.tcl
x <tr bgcolor=#0099FF><td align=center><FONT SIZE='2' COLOR='#FFFFFF' face='verdana'>15&periuk=
(Tcl) 2 % 
w
walker
Voice
Posts: 6
Joined: Thu Sep 03, 2009 1:05 am

Post by walker »

thanks bro raider2k, i must explain what i want to do.

Code: Select all

set arg_date 15

regsub -nocase {.*<a\s+href=\"b.php\">$arg_date(.+)</b>.*} $data {\1} data
 
putquick "PRIVMSG $chan :$data" 
i want to show all text between <a href="b.php">15 to </b> with regsub command.

15 must inside variable arg_date.

please help. thank you so much
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

so you want to show everything between <a href="b.php"> and </b>, but not show the rest of it?

please show me example content of your var $data and what it is meant to look like, Ill try to build something for you.
w
walker
Voice
Posts: 6
Joined: Thu Sep 03, 2009 1:05 am

Post by walker »

raider2k wrote:so you want to show everything between <a href="b.php"> and </b>, but not show the rest of it?

please show me example content of your var $data and what it is meant to look like, Ill try to build something for you.
too much line bro.

i thik how to make <a\s+href="b.php"> and $arg_date to be one

i want to be <a href="b.php">15 how it can be?

15 capture from set arg_date 15 as a value from varible arg_date
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

The most likely issue here is that substitution of the variable named arg_date by its value using $arg_date will not occur because the braces that are surrounding the regsub pattern prevent normal variable, command and backslash substitution.
I must have had nothing to do
w
walker
Voice
Posts: 6
Joined: Thu Sep 03, 2009 1:05 am

Post by walker »

arfer wrote:The most likely issue here is that substitution of the variable named arg_date by its value using $arg_date will not occur because the braces that are surrounding the regsub pattern prevent normal variable, command and backslash substitution.
so? it's imposibble to do that? please help
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

It can be done by various means. Generally I choose the following method of including a variable within a regexp/regsub pattern

Code: Select all

regexp -- [subst -nocommands -nobackslashes {pattern here}] "text here"
The above solution would require that a dollar symbol ($) never occurs elsewhere within the "text here", otherwise it will obviously be assumed to preceed a variable name.

The other alternative would be say to simply exclude the braces and define the pattern with this in mind. For example a \s meaning space would have to be written \\s and square brackets meaning a character set/range would have to be written \[a-z\] etc etc.

People seem to have their favourite methods of dealing with the problem.
I must have had nothing to do
w
walker
Voice
Posts: 6
Joined: Thu Sep 03, 2009 1:05 am

Post by walker »

arfer wrote:It can be done by various means. Generally I choose the following method of including a variable within a regexp/regsub pattern

Code: Select all

regexp -- [subst -nocommands -nobackslashes {pattern here}] "text here"
The above solution would require that a dollar symbol ($) never occurs elsewhere within the "text here", otherwise it will obviously be assumed to preceed a variable name.

The other alternative would be say to simply exclude the braces and define the pattern with this in mind. For example a \s meaning space would have to be written \\s and square brackets meaning a character set/range would have to be written \[a-z\] etc etc.

People seem to have their favourite methods of dealing with the problem.
thanks bro. but 15 is a variable can be 13 or 31 or 30. depend on user request.

so, the script actually for grab user request like !date 15 15 used in regsub for search string in a html.

please help.....

do you have any yahoo messenger?[/b]
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

not sure if the following is what you are looking for, heres a try:

Code: Select all

set arg_date 15

set data "<tr bgcolor=#0099FF><td align=center><FONT SIZE='2' COLOR='#FFFFFF' face='verdana'><a href='b.php'>$arg_date&periuk='"
if { [regexp -all -nocase -- {.+\<a href\=\'(.+?)&periuk} $data -> newurl] } {
		regsub -nocase -all {\>} $newurl " " newurl
		regsub -nocase -all {\'} $newurl "" newurl
		set newurl [split $newurl]
}
set phpfile [lindex $newurl 0]
set arg_date_new [lindex $newurl 1]
puts "<a href=\"$phpfile\">$arg_date_new"
which returns

Code: Select all

(Tcl) 20 % source regextest.tcl
<a href="b.php">15
(Tcl) 20 % 
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

walker wrote:

Code: Select all

regsub -nocase {.*<a\s+href="b.php">$arg_date(.+)</b>.*} $data {\1} data 
Instead of this, you need to keep in mind substitution will not take place within { } bracings. So $arg_data is literally just that, not the contents of the variable named arg_data which prefixing the name with $ usually indicates.

Code: Select all

set dynamic_regexp ".*<a\s+href="b.php">$arg_date(.+)</b>.*"
regsub -nocase -- $dynamic_regexp $data {\1} data 
This is the preferred method of doing it. Use the interpreter to do the substitution for you when creating the variable. Make sure to remember the rules of double-substitution which I will explain. Any escape (\) which needs rendering should be triple (\\\) escaped instead. This means any used for formatting that aren't really a command. All others make sure to escape a single time. This includes special chars "{[\]}'$ among a few others. Using them in the regexp would be as easy as \\\\\"\{\[ which would be substituted to "{[ before being used within the regexp, just keep that in mind. Escapes are special cases and 5 must be used if the sequence is to be rendered after the substitutions. If it were simply {hello}[there] we wanted \{hello\}\[there\] would work and the escape upon the second substitution would be removed. Or, if we wanted the escapes rendered into the regular expression \\\{hello\\\}\\\[there\\\] would produce \{hello\}\[there\] . It's explained much better by others just google "+double substitution +tcl" or "+double evaluation +tcl" and read up, this is how you construct these types of regular expressions.

To me, using [subst] is a crutch of sorts, sure it works but it makes you look foolish and it certainly won't win any speed or beauty races. Using switches to confine [subst]'s natural behavior -nocommands -etc -etc is meant when there is no other alternative to things. Not as a way to "avoid the situation". It's more an avoiding learning more so I can find an easier way and more of the "just stick with what works" approach. Which don't get me wrong, is viable as well. Just not as much so as, possibly... simply using what eggdrop gives you, rather than force substitutions through additional commands. ya know what i mean? :)
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

Your solution will probably not work for two reasons :-

The \s will be substituted in setting the variable and come out as simply s, thus negating its real meaning in the regsub pattern (space character).

The text portion $arg_date(.+) may be interpreted as an array. Since arg_date is not an array, an error will occur.

Thats my reading of it anyway.

I don't think the command subst is foolish in the right circumstances, but like I said, each to his own. The main thing is choosing an option that works.
I must have had nothing to do
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

The \s issue can be avoided by adding an extra \ (i.e. \\s) and to avoid the array conflict use ${arg_date} instead.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

Yes, Sir_Fz, I agree. In fact I pretty much said as much in one of my earlier posts under this thread. Patterns need to be modified to account for them being defined outside braces. Possibly the quotes would need to be double backslashed too, though without testing I'm not certain of that. Even the < and > from html tags look a mite dubious, since these characters have meaning in tcl.

This is exactly why I long ago chose to use the command subst where suitable. I simply made too many silly mistakes in switching between patterns inside braces and patterns outside braces. This is rather more likely to make me look foolish than using an extra command. I can't see an extra few milliseconds execution time as generally being an issue.
I must have had nothing to do
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

The objective here is to have a valid regexp pattern, even though < and > have a special meaning in Tcl they won't be interpreted as such since they're in a string (and not in an expression), a single escape for a quotation is sufficient to return and match it. Basically, there's no need to complicate things, perhaps by trial and error you'll finally reach the desired result (if things get difficult).
Post Reply