• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/03_stdlib/43_regexp

  1 The `regexp()` function compiles the given pattern string into a regular
  2 expression, optionally applying the flags specified in the second argument.
  3 
  4 Throws an exception if unrecognized flag characters are specified or if the
  5 flags argument is not a string value.
  6 
  7 Throws an exception if the given pattern string cannot be compiled into a
  8 regular expression.
  9 
 10 Returns the compiled regexp object.
 11 
 12 -- Testcase --
 13 {%
 14         let re1 = regexp("begin (.+) end", "i");
 15         let re2 = regexp("[a-z]+", "g");
 16         let re3 = regexp("Dots (.+) newlines", "s");
 17 
 18         printf("%.J\n", [
 19                 match("BEGIN this is some text END", re1),
 20                 match("This is a group of words", re2),
 21                 match("Dots now\nmatch\ntext\nwith newlines", re3)
 22         ]);
 23 %}
 24 -- End --
 25 
 26 -- Expect stdout --
 27 [
 28         [
 29                 "BEGIN this is some text END",
 30                 "this is some text"
 31         ],
 32         [
 33                 [
 34                         "his"
 35                 ],
 36                 [
 37                         "is"
 38                 ],
 39                 [
 40                         "a"
 41                 ],
 42                 [
 43                         "group"
 44                 ],
 45                 [
 46                         "of"
 47                 ],
 48                 [
 49                         "words"
 50                 ]
 51         ],
 52         [
 53                 "Dots now\nmatch\ntext\nwith newlines",
 54                 "now\nmatch\ntext\nwith"
 55         ]
 56 ]
 57 -- End --
 58 
 59 
 60 Passing an uncompilable regexp throws an exception.
 61 
 62 -- Testcase --
 63 {%
 64         try {
 65                 // unterminated capture group to trigger syntax error
 66                 regexp("foo(");
 67         }
 68         catch (e) {
 69                 // Massage compile error message for stable output since it is
 70                 // dependant on the underyling C library.
 71                 e.message = "Compile error";
 72                 die(e);
 73         }
 74 %}
 75 -- End --
 76 
 77 -- Expect stderr --
 78 Compile error
 79 In line 10, byte 8:
 80 
 81  `        die(e);`
 82   Near here ---^
 83 
 84 
 85 -- End --
 86 
 87 
 88 Passing an invalid flags argument throws an exception.
 89 
 90 -- Testcase --
 91 {%
 92         regexp(".*", true);
 93 %}
 94 -- End --
 95 
 96 -- Expect stderr --
 97 Type error: Given flags argument is not a string
 98 In line 2, byte 19:
 99 
100  `    regexp(".*", true);`
101   Near here -----------^
102 
103 
104 -- End --
105 
106 -- Testcase --
107 {%
108         regexp(".*", "igz");
109 %}
110 -- End --
111 
112 -- Expect stderr --
113 Type error: Unrecognized flag character 'z'
114 In line 2, byte 20:
115 
116  `    regexp(".*", "igz");`
117   Near here ------------^
118 
119 
120 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt