Document and unit-test regex-capture reset logic (#1451)

* mlr --norc cat was erroring

* Document and unit-test regex-capture reset logic
This commit is contained in:
John Kerl 2023-12-19 09:47:59 -05:00 committed by GitHub
parent b13adbe6c0
commit 4706b4bb78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 0 deletions

View file

@ -78,6 +78,46 @@ GENMD-EOF
* Up to nine matches are supported: `\1` through `\9`, while `\0` is the entire match string; `\15` is treated as `\1` followed by an unrelated `5`.
## Resetting captures
If you use `(...)` in your regular expression, then up to 9 matches are supported for the `=~`
operator, and an arbitrary number of matches are supported for the `match` DSL function.
* Before any match is done, `"\1"` etc. in a string evaluate to themselves.
* After a successful match is done, `"\1"` etc. in a string evaluate to the matched substring.
* After an unsuccessful match is done, `"\1"` etc. in a string evaluate to the empty string.
* You can match against `null` to reset to the original state.
GENMD-CARDIFY-HIGHLIGHT-ONE
mlr repl
[mlr] "\1:\2"
"\1:\2"
[mlr] "abc" =~ "..."
true
[mlr] "\1:\2"
":"
[mlr] "abc" =~ "(.).(.)"
true
[mlr] "\1:\2"
"a:c"
[mlr] "abc" =~ "(.)x(.)"
false
[mlr] "\1:\2"
":"
[mlr] "abc" =~ null
[mlr] "\1:\2"
"\1:\2"
GENMD-EOF
## More information
Regular expressions are those supported by the [Go regexp package](https://pkg.go.dev/regexp), which in turn are of type [RE2](https://github.com/google/re2/wiki/Syntax) except for `\C`:

View file

@ -0,0 +1 @@
mlr -n put -f ${CASEDIR}/mlr

View file

@ -0,0 +1,9 @@
[\1]:[\2]
true
[]:[]
true
[a]:[c]
false
[]:[]
null
[\1]:[\2]

View file

@ -0,0 +1,11 @@
end {
print("[\1]:[\2]");
print("abc" =~ "...");
print("[\1]:[\2]");
print("abc" =~ "(.).(.)");
print("[\1]:[\2]");
print("abc" =~ "(.)x(.)");
print("[\1]:[\2]");
print("abc" =~ null);
print("[\1]:[\2]");
}