Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "can it match using a regex which contains \0" #76

Open
avar opened this issue Jan 8, 2018 · 1 comment
Open

Add "can it match using a regex which contains \0" #76

avar opened this issue Jan 8, 2018 · 1 comment

Comments

@avar
Copy link

avar commented Jan 8, 2018

Since there's no way to pass a NULL character on the command-line, this needs to be done via the tool's ability to read a pattern from a file. Not all matchers can do this.

GNU grep can do it:

$ perl -wE 'say "foo\0bar"' >file; perl -wE 'say "f.*\0[b].*a"' >p; grep -a -f p file; echo $?
foobar
0

Note that some matchers look like they can do it, but they just discard anything on the RHS of the \0, e.g. pcre2grep (even though pcre2 itself supports this):

$ perl -wE 'say "foo\0bar"' >file; perl -wE 'say "f.*\0[x].*a"' >p; ./pcre2grep -a -f p file; echo $?
foobar
0

This match should fail, but doesn't. GNU grep is not fooled by this.

@BurntSushi
Copy link

ripgrep can do it as well:

$ xxd /tmp/corpus
00000000: 666f 6f00 6261 720a                      foo.bar.
$ cat /tmp/pattern
\x00
$ rg -a -f  /tmp/pattern /tmp/corpus
1:foobar

You actually don't even need to use a separate file for ripgrep, since you can utter a NUL byte in ripgrep's native regex syntax, e.g.,

$ rg -a '\u{0}' /tmp/corpus
1:foobar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants