Skip to content

Commit

Permalink
Document and unit-test regex-capture reset logic
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Dec 19, 2023
1 parent 623c6bb commit e5e9d69
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/src/reference-main-regular-expressions.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
1 change: 1 addition & 0 deletions test/cases/dsl-regex-matching/null-reset/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mlr -n put -f ${CASEDIR}/mlr
Empty file.
9 changes: 9 additions & 0 deletions test/cases/dsl-regex-matching/null-reset/expout
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[\1]:[\2]
true
[]:[]
true
[a]:[c]
false
[]:[]
null
[\1]:[\2]
11 changes: 11 additions & 0 deletions test/cases/dsl-regex-matching/null-reset/mlr
Original file line number Diff line number Diff line change
@@ -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]");
}

0 comments on commit e5e9d69

Please sign in to comment.