mIRC Home    About    Download    Register    News    Help

Print Thread
why is regex best? #185922 16/09/07 04:07 AM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Not saying Regex is best. I was always under the impression that it was.

Now, I tell you, Regex is great for picking out things. Instead of mucking around with seperating this or that with gettok or something, I can find it a lot easier with Regex.

I tested it for speed against isin and $left and it was the slowest. I always considered "isin" to not be the best way to compare, but it did way better than regex. Well, relatively.

2714 - $iif($regex(love.heyhey,/love/),yes,no)
2453 - $iif(love isin love.heyhey,yes,no)
2654 - $iif($left(love.heyhey,5) == love.,yes,no)

So what do you think is a good reason to use Regex? Is it a superior way to match information?

I was always under the impression that it was faster. I think I am wrong in that.

Re: why is regex best? [Re: DJ_Sol] #185924 16/09/07 04:38 AM
Joined: Oct 2005
Posts: 1,741
G
genius_at_work Offline
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
Regex allow for infinitely more complex matches than other methods. Regex is slower than the other methods for simple matches because it has to invoke a regex object no matter how simple or complex the regex is. Even simple examples of regex are much smaller than non-regex equivalents. Example:

if ($regex(%var,/(alpha|beta|gamma|delta)/)) blah

vs

if ((alpha isin %var) || (beta isin %var) || (gamma isin %var) || (delta isin %var)) blah


Regex is a very powerful tool if you know how to use it correctly and completely. Very simple comparisons are probably more suitable to using the isin method since they don't invoke a regex object.

For more regex info try: http://www.regular-expressions.info/

-genius_at_work

Re: why is regex best? [Re: genius_at_work] #186006 16/09/07 06:08 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Thanks bro! smile That gives me a better idea how to use regex effectively.

Re: why is regex best? [Re: genius_at_work] #186213 19/09/07 09:41 AM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
Originally Posted By: genius_at_work

if ($regex(%var,/(alpha|beta|gamma|delta)/)) blah

vs

if ((alpha isin %var) || (beta isin %var) || (gamma isin %var) || (delta isin %var)) blah


bad example ;>

Code:
if ($count(%var,alpha,beta,gamma,delta)) blah


regex is powerful, yes, especially when coupled with mirc's nifty $regsubex. but it's a highly overused fad which i see abused a lot

the problem is that scripters get excited after learning something new like regex and tend to fall under its spell. for every task they're given they'll immediately believe regex should be used to solve it. this leads to severe gaps in scripting logic

what i recommend is exhausting every non-regex involved option first, carefully consider if something more suitable exists. then, either as a matter of interest or to improve efficiency by all means go ahead and regexize it! :P

obviously i'm not saying regex should never be used, but if you're interested in actual scripting and discovering scripting techniques (perhaps even developing the skills needed for a programming language) you'll use it less and only when it's suitable to do so


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Re: why is regex best? [Re: DJ_Sol] #187310 03/10/07 06:18 AM
Joined: Mar 2006
Posts: 393
T
The_JD Offline
Fjord artisan
Offline
Fjord artisan
T
Joined: Mar 2006
Posts: 393
Originally Posted By: DJ_Sol
So what do you think is a good reason to use Regex? Is it a superior way to match information?


It can be faster, Generally if you are doing a few comparisons it may be.

What makes me use regex so much these days?
Most programming languages allow the use of regular expressions... allthough some require some slight differences.

Alot of the stuff I write in mIRC, I repeat in PHP.
EG. A script that downloads the local weather.

Thats all IMHO though, Hope this answers your question OP.


[02:16] * Titanic has quit IRC (Excess Flood)
Re: why is regex best? [Re: DJ_Sol] #187359 03/10/07 11:54 PM
Joined: Feb 2005
Posts: 342
R
Rand Offline
Fjord artisan
Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
The situation, is what determines when and why you'd use a regex.

For something as simple as determining if something is in a token delimited line, such as: love.heyhey, you can use a simple:

if ($istok(love.heyhey,love,46)) { blah }
if ($regex(love.heyhey,/love/)) { blah }

The regex doesn't really save you that much space in this example.

Regex is generally to be used when there's a particular pattern that you're looking for, or if you want to look for a pattern, and extract or remove the match.

A simple example of removing the matches:
alias removetags { return $regsubex($1-,/^[^<]*?>|<.*?>|<[^>]*?$/g,) }

//echo -a $removetags(<b>Boldly</b> stripping HTML tags!)


Before using Regex, just ask yourself if there's another way to accomplish what you're doing. Most of the time, $istok / $gettok / *tok, will perform what you want. smile

Re: why is regex best? [Re: jaytea] #187384 04/10/07 02:57 PM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
Quote:

the problem is that scripters get excited after learning something new like regex and tend to fall under its spell. for every task they're given they'll immediately believe regex should be used to solve it. this leads to severe gaps in scripting logic


...or they are pampered by other scripting languages (perl comes to mind!) where regex _is_ the best (or, indeed, only) way to do the comparison.

Not that I would ever succumb to this, of course. *cough* *hack* *cough*


Sais