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

View File

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