yq/pkg/yqlib/doc/String Operators.md

57 lines
638 B
Markdown
Raw Normal View History

2021-01-14 04:05:50 +00:00
# String Operators
2021-01-14 03:46:50 +00:00
2021-07-07 12:40:46 +00:00
## Match string
2021-01-14 03:46:50 +00:00
Given a sample.yml file of:
```yaml
2021-07-07 12:40:46 +00:00
cat
2021-01-14 03:46:50 +00:00
```
then
```bash
2021-07-07 12:40:46 +00:00
yq eval 'match("at")' sample.yml
2021-01-14 03:46:50 +00:00
```
will output
```yaml
2021-07-07 12:40:46 +00:00
string: at
offset: 1
length: 2
captures: []
2021-01-14 03:46:50 +00:00
```
2021-07-07 12:40:46 +00:00
## Match string, case insensitive
2021-01-14 04:05:50 +00:00
Given a sample.yml file of:
```yaml
2021-07-07 12:40:46 +00:00
cAt
2021-01-14 04:05:50 +00:00
```
then
```bash
2021-07-07 12:40:46 +00:00
yq eval 'match("(?i)at")' sample.yml
2021-01-14 04:05:50 +00:00
```
will output
```yaml
2021-07-07 12:40:46 +00:00
string: At
offset: 1
length: 2
captures: []
2021-01-14 04:05:50 +00:00
```
2021-07-07 12:40:46 +00:00
## Match with capture groups
2021-01-14 04:05:50 +00:00
Given a sample.yml file of:
```yaml
2021-07-07 12:40:46 +00:00
a cat
2021-01-14 04:05:50 +00:00
```
then
```bash
2021-07-07 12:40:46 +00:00
yq eval 'match("c(.t)")' sample.yml
2021-01-14 04:05:50 +00:00
```
will output
```yaml
2021-07-07 12:40:46 +00:00
string: cat
offset: 2
length: 3
captures:
- string: at
offset: 3
length: 2
2021-01-14 04:05:50 +00:00
```