Skip to content

Doesn't "?" support non-greedy mode? #1

@babybabycloud

Description

@babybabycloud

Hi,

I have such a code snippet,

const std = @import("std");
const regex = @import("regex");

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();

    var pattern = try regex.Regex.compile(arena.child_allocator, "\\$t\\((.*)\\)");
    const target = "$t(common.hello) abc $t(common.name)";
    try search(arena.child_allocator, target, pattern);
    pattern = try regex.Regex.compile(arena.child_allocator, "\\$t\\((.*?)\\)");
    try search(arena.child_allocator, target, pattern);
}

fn search(allocator: std.mem.Allocator, target: []const u8, re: regex.Regex) !void {
    const matches = try re.findAll(allocator, target);
    for (matches) |m| {
        for (m.captures) |cap| {
            std.debug.print("{s}\n", .{cap});
        }
    }
}

and its output is as below

zig-out/bin/translator
common.hello) abc $t(common.name

It seems that the question mark makes it find nothing.

And what I expect should be "common.hello" and "common.name" (without quote mark).

Is this a issue of zig-regex?

Yes, it does resolve the issue to replace the pattern "\$t\((.)\)" with "\$t\((\S)\)", however it cannot handle "$t(common.hello)$t(common.name)"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions