Well this is a snippet I found that I've done before... It's still in beta stage and any opinion or (constructive) criticism is of course welcome.
The usage is /ask Question here.
Also, /askd will open a dialog which is pretty self-explanatory. Rightclicking in an active window will open it too.
Example /ask Who created The Simpsons?> The Simpsons was created by Matt Groening.
And also, I should probably mention, neither the $urlencode or $chr_r aliases were made by me.
Small update: Added an output if no result was found. Thank you Dean.
Update December 15th: Added a dialog (My first ever made!) with the command /askd to open it ut. Right clicking in an active window and pressing "Ask" will open it too.
alias askd {
if !$dialog(ask) {
dialog -mt ask ask
}
else {
echo -a Dialog already active!
}
}
dialog ask {
title "Ask.com"
size -1 -1 400 180
text "Ask something:", 1, 5 5 500 500
edit "", 2, 5 20 250 20, autohs
button "Ask!", 3, 260 15 30 30
box "Answer", 4, 5 40 370 130
edit "", 5, 10 60 350 100, read multi vsbar
}
on *:dialog:ask:sclick:3:{
if $did(2).text {
ask $v1
}
else {
noop $input(You must specify a question in the editbox!,o,!!!)
}
}
alias ask {
if $1 {
if $sock(ask) {
sockclose $v1
}
sockopen ask www.ask.com 80
sockmark ask $1-
}
else {
echo -a 04Error! Incorrect syntax.
}
}
on *:sockopen:ask:{
var %x = sockwrite -nt $sockname
%x GET /web?qsrc=1&o=0&l=dir&q= $+ $urlencode($sock($sockname).mark) HTTP/1.1
%x Host: www.ask.com
%x Connection: close
%x
}
on *:sockread:ask:{
var %askreader
sockread %askreader
if $regex(%askreader,/<div\s(?:id="r\d+_a"|class="txt3 abstract")\s(?:class="txt3 abstract"|id="r\d+_a")>(.+)(?:<\/div>|<\/span>)|<span class="answers_ui_t\d+">(.+)<\/span>/U) {
$iif($dialog(ask),did -ri ask 5 1,echo -a 03 $+ ) $iif($dialog(ask),$strip($char_r($replace($regml(1),<b>,,</b>,,","))),$char_r($replace($regml(1),<b>,,</b>,,",")))
sockclose $sockname
}
if (*did not match* iswm %askreader) {
$iif($dialog(ask),did -ri ask 5 1,echo -a 04 $+ ) No result for your query $qt($sock($sockname).mark) was found.
sockclose $sockname
}
}
alias urlencode return $regsubex($1,/(\W)/gS,% $+ $base($asc(\1),10,16,2)))
alias char_r return $regsubex($1-,/&#(\d+);/g,$chr(\1))
menu * {
Ask:askd
}
he already did (although '\v' shouldn't be included there)Indeed! I quickly pasted something from wikipedia, but it was about POSIX character class, my bad, here is a quote from pcre.txt:
For compatibility with Perl, \s does not match the VT character (code
11). This makes it different from the the POSIX "space" class. The \s
characters are HT (9), LF (10), FF (12), CR (13), and space (32). If
"use locale;" is included in a Perl script, \s may match the VT charac-
ter. In PCRE, it never does.
why not share your know-how in \s so everybody can learn from what you've learned?
he already did (although '\v' shouldn't be included there):
\s match a white space, same as [:space:] or [ \t\r\n\v\f] not just a $chr(32)
from there, you should be expected to realize that an IRC message may include \t and \f, therefore using \s in place of a regular space, when you mean to match a space and only a space, is incorrect.
Sure, but the question wasn't specific to that regex Dean.
Jethro: it was sarcastic, and the truth, i've read enough posts to know that
Note that you answered my post by saying the same mistake again "use \s to indicate a space", again if people want a space, they should use a space, there's no "I like to", everyone likes to use the proper code they need lol
Wims, why wrote this:> you guys are too busy to show to hawkee's users who knows more than the other :)This sounds sarcastic. We're here to help as much as we can, not to show arrogant attitude toward one another. While some info may be misleading based on our learning, you're always welcome to come forward and correct it with a humble courtesy.
??
I don't feel uncomfortable with anything here and i use a space to indicate a space, \s isn't a space.
Someone asked what was \s in regex, someone else said it was the space character, this is simply not true and i'm correcting what the person said before anyone start believing it, you know, you guys are too busy to show to hawkee's users who knows more than the other :)
You can use the look-behind or look-ahead assertions to demand a targeted match to be met (you may want to read up on those assertions to gain understanding of how they work) , while the
(?U)
,
(.+?)
and
/U
are telling regex to match as few strings as possible, being less greedy. Regex by design is greedy and case-sensitive.
i just mixed both regex match, and i used ?: wich will not save the reference to the $regml so in this case, it will have only $regml(1) and not $regml(2) and $regml(3) as in your case
since you have used (.*?) so i used the /U modifier, which will make all the references not greedy
if you have any doubt ask me
$regex(%askreader,/<div\s(?:id="r\d+_a"|class="txt3 abstract")\s(?:class="txt3 abstract"|id="r\d+_a")>(.+)(?:<\/div>|<\/span>)|<span class="answers_ui_t\d+">(.+)<\/span>/U) {
since you have asked it Kiddo96, i think you can use one regex match instead 2
this is a simple example, and i added the $replace for " to "
and a check to check if the results match
alias ask {
if $1 {
if $sock(ask) {
sockclose $v1
}
sockopen ask www.ask.com 80
sockmark ask $1-
}
else {
echo -a 04Error! Incorrect syntax.
}
}
on *:sockopen:ask:{
var %x = sockwrite -nt $sockname
%x GET /web?qsrc=1&o=0&l=dir&q= $+ $urlencode($sock($sockname).mark) HTTP/1.1
%x Host: www.ask.com
%x Connection: close
%x
}
on *:sockread:ask:{
var %askreader
sockread %askreader
if $regex(%askreader,/<div\s(?:id="r\d+_a"|class="txt3 abstract")\s(?:class="txt3 abstract"|id="r\d+_a")>(.+)(?:<\/div>|<\/span>)|<span class="answers_ui_t\d+">(.+)<\/span>/U) {
echo -a 09 $+ $char_r($replace($regml(1),<b>,,</b>,,","))
sockclose $sockname
}
if (*did not match* iswm %askreader) { echo -ag 09 Your search for $qt($sock($sockname).mark) did not match with any Web results. | sockclose $sockname
}
}
alias urlencode return $regsubex($1,/(\W)/gS,% $+ $base($asc(\1),10,16,2)))
alias char_r return $regsubex($1-,/&#(\d+);/g,$chr(\1))
i didnt tried another thing, but i think we can improve a lot
if i think in something i will post it