ReviewOS

zig-utils/zig-regex

Pattern “[a\-c]” interpreted as “[a-c]”

#22
Open Viorel opened this 8 days ago ·
8 days ago

The pattern [a\-c] means “a”, “-” or “c”. However, it seems to be interpreted as [a-c], i.e. a letter between “a” and “c” inclusive. The next fragment demonstrates the problem:

// Using zig-regex 0.2.1 and Zig 0.17.0-dev.1818+7051f8e73

const pattern = "[a\\-c]";

std.debug.print( "pattern: {s}\n", .{pattern}); // prints "[a\-c]" as expected

var regex = try Regex.compile( allocator, pattern);

std.debug.print( "isMatch: {any}\n", .{regex.isMatch( "a")}); // "true" as expected
std.debug.print( "isMatch: {any}\n", .{regex.isMatch( "-")}); // "false", should be "true"
std.debug.print( "isMatch: {any}\n", .{regex.isMatch( "b")}); // "true", should be "false"
std.debug.print( "isMatch: {any}\n", .{regex.isMatch( "c")}); // "true" as expected

If the constant is replaced with "[ac\\-]" or "[ac-]", or the unicode or unicode_sets flags are specified, or another engine is used, then it works as expected.

Sign in to comment on this issue.