blob: 09f0e3ec4ec631f9815fc2865c7dfcee2c7ec15e [file] [log] [blame] [view]
Marcel van Lohuizen0117d902019-02-21 23:55:39 +01001[TOC](Readme.md) [Prev](rangedef.md) [Next](lists.md)
2
3_Expressions_
4
5# Regular expressions
6
7The `=~` and `!~` operators can be used to check against regular expressions.
8
9The expression `a =~ b` is true if `a` matches `b`, while
10`a !~ b` is true if `a` does _not_ match `b`.
11
N483afc52019-08-18 14:55:25 +000012Just as with comparison operators, these operators may be used
Marcel van Lohuizen0117d902019-02-21 23:55:39 +010013as unary versions to define a set of strings.
14
15
16<!-- CUE editor -->
Marcel van Lohuizenf0c94042019-02-22 22:46:37 +010017_regexp.cue:_
Marcel van Lohuizen0117d902019-02-21 23:55:39 +010018```
19a: "foo bar" =~ "foo [a-z]{3}"
20b: "maze" !~ "^[a-z]{3}$"
21
22c: =~"^[a-z]{3}$" // any string with lowercase ASCII of length 3
23
24d: c
25d: "foo"
26
27e: c
28e: "foo bar"
29```
30
31<!-- result -->
Marcel van Lohuizenf0c94042019-02-22 22:46:37 +010032`$ cue eval -i regexp.cue`
Marcel van Lohuizen0117d902019-02-21 23:55:39 +010033```
34a: true
35b: true
Marcel van Lohuizen00c373d2019-06-07 01:08:23 +020036c: =~"^[a-z]{3}$"
Marcel van Lohuizen0117d902019-02-21 23:55:39 +010037d: "foo"
Marcel van Lohuizenf06f7812019-07-17 17:28:48 +020038e: _|_ /* invalid value "foo bar" (does not match =~"^[a-z]{3}$") */
N483afc52019-08-18 14:55:25 +000039```