bug: matches[1]

Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

bug: matches[1]

Post by Caled »

Code: Select all

Regex pattern: ^H:(\d+)/(\d+)  E:(\d+)/(\d+)\.$
Script:
echo(matches[2])
Mud output:
H:4661/4661 E:22205/22205.H:4661/4661 E:22205/22205.
The bolded part is the echo from the trigger. When I use
matches[2] instead, the expected "4661" is displayed.

matches[1] seems to be matching the entire trigger line. This is not my prompt, just to be clear on that (it is part of the output from the `atcp command in mudbot.)

Bug, intended or something wrong with my pattern?

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: bug: matches[1]

Post by Heiko »

This is not a bug. The first item in the string list matches, matches[1] contains the first *full match*, i. e. the first complete match of your regex pattern - or if you are using substring/begin of line substing type of triggers, matches[1] contains the pattern itself.

To get the entire line you'd have to reference the variable "line".
Example: If we get this line from the MUD
"Tom gives Jim a ride to Tom's house."
The regex pattern Jim a (ride) to (\w+)
will get you this:

Code: Select all

line = "Tom gives Jim a ride to Tom's house."
matches[1] = "Jim a ride to Tom"
matches[2] = "ride"
matches[3] = "Tom"
Another example:

Line is: Tom gives Jim a ride to Terry's house.
pattern: T(\w+)
without checking the Perl /g switch option you'd get:

Code: Select all

line = "Tom gives Jim a ride to Terry's house."
matches[1] = "Tom"
matches[2] = "Tom" <---- first capture group of first match in the line
With the Perl /g switch option you'd get

Code: Select all

line = "Tom gives Jim a ride to Terry's house."
matches[1] = "Tom"
matches[2] = "Tom"
matches[3] = "Terry" <---- second *complete* match of pattern "T(\w+)"
matches[4] = "Terry" <---- first capture group of second regex match on this line

Post Reply