mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Aug 2005
Posts: 1,052
L
Lpfix5 Offline OP
Hoopy frood
OP Offline
Hoopy frood
L
Joined: Aug 2005
Posts: 1,052
I keep looking at various post and I don't understand the moethod of regex and what its used for so far It seems to strip down a certain word in a word but then again im not sure, and whens the best time to use it?

and then a call to $regml

Even the mIRC example has me stumped

even time to time ill see something like $regex(word,/\@?#\/) or something like that its used in a variety of scripts so this $regex feature must be something great to be using often.

Its ok if you dont want to post a small tut on it I just wanted to learn those calls from mirc

Joined: Mar 2003
Posts: 612
B
Pan-dimensional mouse
Offline
Pan-dimensional mouse
B
Joined: Mar 2003
Posts: 612


billythekid
Joined: Aug 2005
Posts: 1,052
L
Lpfix5 Offline OP
Hoopy frood
OP Offline
Hoopy frood
L
Joined: Aug 2005
Posts: 1,052
ok so a wildcard on steriods.

Can I please have an example of something that would occur in mirc that I would need this

N
netsplit
netsplit
N
This is an old code segment I wrote for a friend that uses regsubex to find/replace acronyms awhile back.

alias scannreplace {
var %scnt = $1-
var %p = 1
while (%p < 10) {
%scnt = $regsubex(flol, %scnt ,lol,%loltext)
%scnt = $regsubex(flmao, %scnt ,lmao,%lmaotext)
%scnt = $regsubex(flmfao, %scnt ,lmfao,%lmfaotext)
%scnt = $regsubex(fhaha, %scnt ,haha,%hahatext)
%scnt = $regsubex(fbrb, %scnt ,brb,%brbtext)
%scnt = $regsubex(fwb, %scnt ,lwb,%wbtext)
%scnt = $regsubex(fyw, %scnt ,yw,%ywtext)
%scnt = $regsubex(fty, %scnt ," ty", %tytext)
inc %p
}
return %scnt
}

That's horribly redundant I think Ima recode it tonight and swap the globals for an ini loaded hash XD.

Not sure where just $regex alone would be used, maybe for database handling? if ( $mid($1,0,1) != / ) works for checking if an input is a command or a message better then regex would so don't know.

Never needed it before.


//echo  $regex(regtext,haha lol haha lol haha lol haha,haha)

returns 1? o.O


how about
(untested)

Code:
on *:TEXT:*:#room:{
if ($regex($1-,http://) &gt; 0) {
kick $chan $nick NO SPAMMING
}
}

Last edited by netsplit; 05/09/06 04:08 AM.
Joined: Feb 2006
Posts: 523
J
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 523
seems the only examples of regex you've encountered involve no real regex specific techniques smile incidentally, assuming that by "ty" you meant ty and by lwb you meant wb such that the acronyms you're replacing are all the front of their corresponding global variable names... you can in fact shorten that to:

Code:
alias scanreplace return $regsubex($1,/\b(lol|lmf?ao|haha|brb|wb|yw|ty)\b/[color:blue]g[/color]i,$(%\1text,2))


i guess you looped in your code to cover all replacements in the string, where adding the g modifier would've sufficed laugh a pipe, |, represents "or" in regex.. so the group matches lol OR lmf?ao OR haha etc. then \b matches a position, where a word character (a-zA-Z0-9_) matches a non word character (everything else) and vice versa. this is to avoid matching acronyms in the middle of other words

after the match is made we replace it with $(%\1text,2), where \1 is substituted by each acronym that was matched in turn, which firstly builds the variable '%<acronym>text' then evaluates the result to get you the expansion


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Joined: Aug 2005
Posts: 1,052
L
Lpfix5 Offline OP
Hoopy frood
OP Offline
Hoopy frood
L
Joined: Aug 2005
Posts: 1,052
Ok you have given a good example the only problem i still dont understand the forumla your using and whats up with the commands in between

was that a method of creating a loop in the $regsubex for the text or something

Or ??

Yes I know I dont make sense lol this $regxxxxxxxxx commands are mind boggling when your new to them.

N
netsplit
netsplit
N
Wow that's pretty cool.

N
netsplit
netsplit
N
Quote:
Ok you have given a good example the only problem i still dont understand the forumla your using and whats up with the commands in between

was that a method of creating a loop in the $regsubex for the text or something

Or ??

Yes I know I dont make sense lol this $regxxxxxxxxx commands are mind boggling when your new to them.


$regsubex($1,/\b(lol|lmf?ao|haha|brb|wb|yw|ty)\b/gi,$(%\1text,2))

$1- is the data sent to the variable. The \b on either side check for spaces. No spaces no match. (text|moretext|othertext) means either text moretext or othertext in that spot works for a match. /gi are regex arguments. g means to repeat for all on the line. So a loop in a way. Not sure what i does. $(%\1text,2) combines the text that was a match to get the variable. So if moretext was a match it'd come out as %moretexttext and to the user it'd come out was what ever data the %moretexttext variable held.

If I understand it correctly anyway. Thanks jaytea. This is going to be fun to play with. Now I gotta figure out if sleep or coding is more important tonight.

Couple questions. What does the / at the begining do?, what does the i do? and what does the ,2 do?

Joined: Aug 2005
Posts: 1,052
L
Lpfix5 Offline OP
Hoopy frood
OP Offline
Hoopy frood
L
Joined: Aug 2005
Posts: 1,052
I assume if i got this correct the / make sure its not a command performed i can be wrong

Joined: Oct 2005
Posts: 1,671
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,671
Regular expressions are usually surrounded by / slashes. It is done in order to keep the 'switches' separate from the regular expression (the match text).

In PERL, the most common regex commands were like this:

m/(regex match here)/(switches)

and

s/(regex match here)/(substitute text here)/(switches)


Here are some examples of regexes:

Code:
alias df.check {
  var %date.fmt = $1-
  if ($regex(date,%date.fmt,/(^\s*$|d{5,}|m{5,}|y{5,}|(?&lt;!y)y{3}(?!y)|o{3,}|(?&lt;!o)o(?!o)|(?:[^d]|d{3,}|^)oo|[^dmoy -/\\])/)) echo 4 -a Invalid format: $regml(date,1)
  else echo 3 -a Valid format
}

alias tf.check {
  var %time.fmt = $1-
  if ($regex(time,%time.fmt,/(^\s*$|[hH]{3,}|hH|Hh|n{3,}|s{3,}|[tT]{3,}|tT|Tt|[^z]{4,}|[^hHnstTz :-])/)) echo 4 -a Invalid format: $regml(time,1)
  else echo 3 -a Valid format
}

The above code is used to determine if date/time formats (used in $asctime) are valid.

Looking at this code:
Code:
/(^\s*$|[hH]{3,}|hH|Hh|n{3,}|s{3,}|[tT]{3,}|tT|Tt|[^z{4,}|[^hHnstTz :-])/

It is broken down like this:
Code:
[1]: A numbered capture group. [^\s*$|[hH]{3,}|hH|Hh|n{3,}|s{3,}|[tT]{3,}|tT|Tt|[^z]{4,}|[^hHnstTz :-]]
    Select from 11 alternatives
        ^\s*$
            Beginning of line or string
            Whitespace, any number of repetitions
            End of line or string
        Any character in this class: [hH], at least 3 repetitions
        hH
        Hh
        n, at least 3 repetitionss, at least 3 repetitions
        Any character in this class: [tT], at least 3 repetitions
        tT
        Tt
        Any character other than z, at least 4 repetitions
        Any character that is not in this class: [hHnstTz :-]
/


The way the code is written, if the regex matches, then the date/time format is INvalid.


There is no shortage of Regex tutorials or examples on the internet. The easiest way to learn is to read the tutorials and then try the examples. Then try your own regexes. Find some data that has a recognizable format, but also has parts that change (ex. time, date, etc) and then make a regex that will match it, even when the data changes.

-genius_at_work


Link Copied to Clipboard