mIRC Home    About    Download    Register    News    Help

Print Thread
Regular Expression Help file problem #189932 13/11/07 06:18 PM
Joined: May 2007
Posts: 89
T
TropNul Offline OP
Babel fish
OP Offline
Babel fish
T
Joined: May 2007
Posts: 89
I've installed mIRC6.31 (haven't ever used the 6.3) and started reading the help file.

I think there's an error or instead, a misunderstanding statement on the page discussing about Regular Expressions.

The help file says:
Quote:

$regex([name], text, re)

Returns N, the number of strings in text that matched the regular expression.


That's false and should be imo:

Returns 0 or 1 on whether the regular expression didn't match or matched 'text'. (or something like that)

However, the statement of the help file refers to the /g modifier. Maybe this should be documented or modified.

There's the same issue for $regsub.

Quote:

$regsub([name], text, re, subtext, %var)

Performs a regular expression match, like $regex(), and then performs a substitution using subtext.

Returns N, the number of substitutions made, and assigns the result to %var.


Again, that's true if and only the /g modifier is included.

Regards


tropnul
Re: Regular Expression Help file problem [Re: TropNul] #189934 13/11/07 06:30 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
I'm not sure I get your point. Without the /g modifier the only number of matches possible are 0 and 1, and so that's what $regex/$regsub return.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: Regular Expression Help file problem [Re: starbucks_mafia] #189936 13/11/07 06:36 PM
Joined: May 2007
Posts: 89
T
TropNul Offline OP
Babel fish
OP Offline
Babel fish
T
Joined: May 2007
Posts: 89
It's not mentioned in the help file that you must use the /g modifier so as to get the total number of matches. Instead, the help says that, the identifiers $regex and $regsub returns the numbers of matches. I think that's false and should be modified and said to return only 0 or 1 by default and returns the number of matches only if the modifier /g is included.

Personally, this doesn't bother me. It's just that for regular expression beginners, this could be misunderstanding. And also, the statements are false.

frown


tropnul
Re: Regular Expression Help file problem [Re: TropNul] #189937 13/11/07 06:42 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Quote:
[...] the help says that, the identifiers $regex and $regsub returns the numbers of matches.

Which is exactly what they do. The /g modifier is part of the regular expression, without which that regular expression will only ever match once at most. It's not something that mIRC is doing that would be unexpected to someone familiar with regular expressions, it is an underlying feature of the PCRE syntax that an expression without the modifier will only match once. Naturally if you're using the regular expression identifiers at all it is assumed you know how to use them.

Now, whether it would be handy to have a simple regular expression reference made available in the mIRC help file, that's another matter. But the documentation at it stands is correct in what it says.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: Regular Expression Help file problem [Re: starbucks_mafia] #189939 13/11/07 06:54 PM
Joined: May 2007
Posts: 89
T
TropNul Offline OP
Babel fish
OP Offline
Babel fish
T
Joined: May 2007
Posts: 89
Quote:

Quote:
[...] the help says that, the identifiers $regex and $regsub returns the numbers of matches.

Which is exactly what they do.


That's false. A matching regexp without /g will never return the number of matches. It'll return 1 or 0 if non-matched.

I mean, you really think that what the help file says is correct ? I must have missed something or whatever ...

Well, nevermind, it seems that my post was useless.


tropnul
Re: Regular Expression Help file problem [Re: starbucks_mafia] #189942 13/11/07 07:04 PM
Joined: May 2007
Posts: 89
T
TropNul Offline OP
Babel fish
OP Offline
Babel fish
T
Joined: May 2007
Posts: 89
Huh, I thought I saw a reply but it has been deleted maybe. That reply did not convinced me. Maybe i'm wrong but i'll stick to this opinion. sorry. wink


tropnul
Re: Regular Expression Help file problem [Re: TropNul] #189954 14/11/07 02:35 AM
Joined: Oct 2005
Posts: 1,741
G
genius_at_work Offline
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
The help file is correct (although unspecific) in this case.

Ignoring mIRC's identifiers, and focusing on PCRE, the following is true:

-----------------------------

Text: AAA
Regex: /A/
Matches: 1

Using the above regular expression yields exactly 1 match. Once the PCRE engine finds the first exact match within the text, it immediately stops searching. Therefore, the number of matches is exactly 1.

------------------

Text: AAA
Regex: /A/g
Matches: 3

Using this regex, the g switch, (which is part of the regex, NOT part of mIRC), tells the PCRE engine to search again in the text that was not part of the previous matches. The engine continues searching until no further matches can be made in the text.


----------------------

mIRC simply returns whatever the PCRE engine provides as a 'total matches' result.

-genius_at_work

Re: Regular Expression Help file problem [Re: genius_at_work] #190062 15/11/07 03:16 PM
Joined: May 2007
Posts: 89
T
TropNul Offline OP
Babel fish
OP Offline
Babel fish
T
Joined: May 2007
Posts: 89
Alright, now I understand that I was at fault.
Indeed, the help file is correct. I got mingled in some things that made me argue for nothing :s.

Thank you for the clear explanation :').


tropnul