Page 1 of 1

regex unexpected match of multiple chars in { brackets }

Posted: Mon Mar 07, 2011 5:52 pm
by badhack
Consider these two lines of code:

$gamestatscache{$player}{$svariant}{wins} = 0;
$gamestatscache{$player}{$svariant}{losses} = 0;

In Textpad 5.4.2 I want to match the "$gamestatscache{$player}{" part which did not work. To simplify the problem I tried to match "$gamestatscache{$player" with:

REGEX 1: $gamestatscache{$[:alpha:]+
REGEX 1a: $gamestatscache{$\([:alpha:]\)+

Both

MATCH 1: $gamestatscache{$pla

which is wrong, correct match would be

MATCH 1b: $gamestatscache{$player

I tried to work around the problem with

REGEX 2: $gamestatscache{$[:alpha:]+yer

which matches (somewhat consequentially)

MATCH: $gamestatscache{$player

Seeing that, I tried to go for the full nine yards with

REGEX2: $gamestatscache{$[:alpha:]+yer}

which fails (no matches).

I am not sure if I got the syntax of Textpad's regex implementation right (I am mostly using perl regexes) but this looks like a bug to me.

Thank you for looking into this
from a rather happy registered & licensed user.

Posted: Mon Mar 07, 2011 6:43 pm
by ben_josephs
What you tried wouldn't work in Perl either. Try
\$gamestatscache{\$[[:alpha:]]+}{
or
\$gamestatscache{\$[a-z]+}{

Posted: Mon Mar 07, 2011 7:30 pm
by badhack
ben_josephs wrote:What you tried wouldn't work in Perl either. Try
\$gamestatscache{\$[[:alpha:]]+}{
or
\$gamestatscache{\$[a-z]+}{
Thanks, I know that Perl syntax is different ;)

I keep forgetting that [:alpha:]+ does not work in TextPad as one might expect but that they are character class operators only. Also, Textpad seems to treat $ as a normal character if the regex does not contain a preceding ^ (that's why this example also works without escaping $)

Thank you kindly for your reply, it pushed me in the right direction.

Posted: Mon Mar 07, 2011 11:06 pm
by ben_josephs
[:alpha:]+ does not work anywhere as you expect. The notation [:alpha:] can only be used inside a character class: you must enclose it in another pair of square brackets: [[:alpha:]].

Are you sure about TextPad's handling of $? I'm using TextPad 5.3, and in that version, when Regular expression is selected, $ is treated as an end-of-line anchor, wherever it occurs (except when it's quoted or is in a character class).