Improving reg ex doc

This commit is contained in:
Mike Farah 2021-07-16 10:18:25 +10:00
parent 2f0fe8aa13
commit 171ca2e053
2 changed files with 58 additions and 62 deletions

View File

@ -45,18 +45,18 @@ foo bar FOO
``` ```
then then
```bash ```bash
yq eval 'match("(?i)foo"; "g")' sample.yml yq eval '[match("(?i)foo"; "g")]' sample.yml
``` ```
will output will output
```yaml ```yaml
string: foo - string: foo
offset: 0 offset: 0
length: 3 length: 3
captures: [] captures: []
string: FOO - string: FOO
offset: 8 offset: 8
length: 3 length: 3
captures: [] captures: []
``` ```
## Match with capture groups ## Match with capture groups
@ -66,24 +66,24 @@ abc abc
``` ```
then then
```bash ```bash
yq eval 'match("(abc)+"; "g")' sample.yml yq eval '[match("(abc)+"; "g")]' sample.yml
``` ```
will output will output
```yaml ```yaml
string: abc - string: abc
offset: 0 offset: 0
length: 3 length: 3
captures: captures:
- string: abc - string: abc
offset: 0 offset: 0
length: 3 length: 3
string: abc - string: abc
offset: 4 offset: 4
length: 3 length: 3
captures: captures:
- string: abc - string: abc
offset: 4 offset: 4
length: 3 length: 3
``` ```
## Match with named capture groups ## Match with named capture groups
@ -93,26 +93,26 @@ foo bar foo foo foo
``` ```
then then
```bash ```bash
yq eval 'match("foo (?P<bar123>bar)? foo"; "g")' sample.yml yq eval '[match("foo (?P<bar123>bar)? foo"; "g")]' sample.yml
``` ```
will output will output
```yaml ```yaml
string: foo bar foo - string: foo bar foo
offset: 0 offset: 0
length: 11 length: 11
captures: captures:
- string: bar - string: bar
offset: 4 offset: 4
length: 3 length: 3
name: bar123 name: bar123
string: foo foo - string: foo foo
offset: 12 offset: 12
length: 8 length: 8
captures: captures:
- string: null - string: null
offset: -1 offset: -1
length: 0 length: 0
name: bar123 name: bar123
``` ```
## Capture named groups into a map ## Capture named groups into a map
@ -154,18 +154,18 @@ cat cat
``` ```
then then
```bash ```bash
yq eval 'match("cat"; "g")' sample.yml yq eval '[match("cat"; "g")]' sample.yml
``` ```
will output will output
```yaml ```yaml
string: cat - string: cat
offset: 0 offset: 0
length: 3 length: 3
captures: [] captures: []
string: cat - string: cat
offset: 4 offset: 4
length: 3 length: 3
captures: [] captures: []
``` ```
## Test using regex ## Test using regex

View File

@ -24,28 +24,25 @@ var stringsOperatorScenarios = []expressionScenario{
{ {
description: "Match string, case insensitive", description: "Match string, case insensitive",
document: `foo bar FOO`, document: `foo bar FOO`,
expression: `match("(?i)foo"; "g")`, expression: `[match("(?i)foo"; "g")]`,
expected: []string{ expected: []string{
"D0, P[], ()::string: foo\noffset: 0\nlength: 3\ncaptures: []\n", "D0, P[], (!!seq)::- string: foo\n offset: 0\n length: 3\n captures: []\n- string: FOO\n offset: 8\n length: 3\n captures: []\n",
"D0, P[], ()::string: FOO\noffset: 8\nlength: 3\ncaptures: []\n",
}, },
}, },
{ {
description: "Match with capture groups", description: "Match with capture groups",
document: `abc abc`, document: `abc abc`,
expression: `match("(abc)+"; "g")`, expression: `[match("(abc)+"; "g")]`,
expected: []string{ expected: []string{
"D0, P[], ()::string: abc\noffset: 0\nlength: 3\ncaptures:\n - string: abc\n offset: 0\n length: 3\n", "D0, P[], (!!seq)::- string: abc\n offset: 0\n length: 3\n captures:\n - string: abc\n offset: 0\n length: 3\n- string: abc\n offset: 4\n length: 3\n captures:\n - string: abc\n offset: 4\n length: 3\n",
"D0, P[], ()::string: abc\noffset: 4\nlength: 3\ncaptures:\n - string: abc\n offset: 4\n length: 3\n",
}, },
}, },
{ {
description: "Match with named capture groups", description: "Match with named capture groups",
document: `foo bar foo foo foo`, document: `foo bar foo foo foo`,
expression: `match("foo (?P<bar123>bar)? foo"; "g")`, expression: `[match("foo (?P<bar123>bar)? foo"; "g")]`,
expected: []string{ expected: []string{
"D0, P[], ()::string: foo bar foo\noffset: 0\nlength: 11\ncaptures:\n - string: bar\n offset: 4\n length: 3\n name: bar123\n", "D0, P[], (!!seq)::- string: foo bar foo\n offset: 0\n length: 11\n captures:\n - string: bar\n offset: 4\n length: 3\n name: bar123\n- string: foo foo\n offset: 12\n length: 8\n captures:\n - string: null\n offset: -1\n length: 0\n name: bar123\n",
"D0, P[], ()::string: foo foo\noffset: 12\nlength: 8\ncaptures:\n - string: null\n offset: -1\n length: 0\n name: bar123\n",
}, },
}, },
{ {
@ -76,10 +73,9 @@ var stringsOperatorScenarios = []expressionScenario{
{ {
description: "Match with global flag", description: "Match with global flag",
document: `cat cat`, document: `cat cat`,
expression: `match("cat"; "g")`, expression: `[match("cat"; "g")]`,
expected: []string{ expected: []string{
"D0, P[], ()::string: cat\noffset: 0\nlength: 3\ncaptures: []\n", "D0, P[], (!!seq)::- string: cat\n offset: 0\n length: 3\n captures: []\n- string: cat\n offset: 4\n length: 3\n captures: []\n",
"D0, P[], ()::string: cat\noffset: 4\nlength: 3\ncaptures: []\n",
}, },
}, },
{ {