matches[n] and the ? regexp operator

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

matches[n] and the ? regexp operator

Post by Caled »

Code: Select all

Alias:
^boilp ((\d+)\s)?(\w+)$

Script:
echo("\n 1: " .. matches[2])
echo("\n 2: " .. matches[3])
echo("\n 3: " .. matches[4] .. "\n")
The following is the output when I test this. The command echo is in italics.
boilp 23 health

1: 23
2: 23
3: health

boilp health

1: health
It is my understanding that using the optional ? character around a capture group should not change the number the other capture groups are assigned to, should that optional value not be present in the string..

In other words, the expected output was:

1:
2:
3: health

In fact, it is my understanding that in the past in Mudlet - it worked as expected. I can say with absolute certainty that regexp in CMUD works this way.

I'm confused!

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

Re: matches[n] and the ? regexp operator

Post by Heiko »

Your pattern ^boilp ((\d+)\s)?(\w+)$ matches your input "boilp 23 health" - and all 3 capture groups contain data:
group 1 (23) pattern =(\d+)
group 2 (23 ) pattern=(\d+\s)
group 3 (health)

The ? operator means in this context: If there is a number after "^boiltp\s" the number may be followed by a space. This behaviour is perfectly correct. I directly use the pcre built-in functions to determine the number of capture groups, their IDs and capture group data retrieval. Consquently, Mudlet's behaviour in this respect will always be the built-in pcre engine behaviour. I don't think that cmud does anything different in this respect as it wouldn't be according to the pcre specs.

By the way: We have a nice new printTable( tableName ) function that prints out tables of any complexity in a nice readable format.

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

Re: matches[n] and the ? regexp operator

Post by Caled »

I think you missed my point, but I also wasn't too clear. Lets simplify it.

Pattern: ^test (\d+ )?(\w+)$

Type in:
test 123 string
test string

Compare the contents of matches[2] and matches[3] in each case.
In the screenshots that follow, I'm showing what CMUD does, and what I believe Mudlet has done in the past.
Also, what Mudlet is not doing now. Mudlet is putting "string" into matches[2], instead of leaving matches[2] as a nil value, and "string" into matches[3] both times.
Attachments
regexptest2.PNG
regexptest2.PNG (13.7 KiB) Viewed 2825 times
regexptest1.PNG
regexptest1.PNG (18.53 KiB) Viewed 2825 times

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

Re: matches[n] and the ? regexp operator

Post by Heiko »

I can see your point now. I think that your request is justified and I'll put it on my todo list. ETA beta-15.

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

Re: matches[n] and the ? regexp operator

Post by Heiko »

I forgot to include a fix for this issue in beta-15. The fix is now available in git though.
I have chosen not to use undefined keys, but empty strings for empty capture groups in order to make scripting easier.
Consequently, for the pattern ^testCapture (\d+ )?(\w+)$ we get:

testCapture 23 test

table {
1: 'testCapture 23 test'
2: '23 '
3: 'test'
}
testCapture test

table {
1: 'testCapture test'
2: ''
3: 'test'
}

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

Re: matches[n] and the ? regexp operator

Post by Caled »

Great :) Thanks Heiko

Post Reply