Skip to content

using '^' (or '\A') with multi-line search doesn't always produce expected result #1878

@BurntSushi

Description

@BurntSushi

Initially found on HN: https://news.ycombinator.com/item?id=27324265

Specifically, while this output is correct (since ^ is set to be in regex multi-line mode always):

$ printf 'a\nbaz\nabc\n' | rg -U '^b'
baz

It should be the case that using (?-m)^b or \Ab would not print baz as a match. But that's not the case here:

$ printf 'a\nbaz\nabc\n' | rg -U '(?-m)^b'
baz
$ printf 'a\nbaz\nabc\n' | rg -U '\Ab'
baz

The issue here is that in this case, ripgrep isn't memory mapping the input. In that case, ripgrep tries to be "smart" and not actually read the entire contents on to the heap if it knows the pattern can't match through a line terminator. But in this case, we can't quite make that assumption since anchors can match line terminators as look-around.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions