summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/43_regexp
blob: 9809c87a722a7b179d8de9971344d298576b2cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
The `regexp()` function compiles the given pattern string into a regular
expression, optionally applying the flags specified in the second argument.

Throws an exception if unrecognized flag characters are specified or if the
flags argument is not a string value.

Throws an exception if the given pattern string cannot be compiled into a
regular expression.

Returns the compiled regexp object.

-- Testcase --
{%
	let re1 = regexp("begin (.+) end", "i");
	let re2 = regexp("[a-z]+", "g");
	let re3 = regexp("Dots (.+) newlines", "s");

	printf("%.J\n", [
		match("BEGIN this is some text END", re1),
		match("This is a group of words", re2),
		match("Dots now\ndon't\nmatch\ntext\nwith newlines", re3)
	]);
%}
-- End --

-- Expect stdout --
[
	[
		"BEGIN this is some text END",
		"this is some text"
	],
	[
		[
			"his"
		],
		[
			"is"
		],
		[
			"a"
		],
		[
			"group"
		],
		[
			"of"
		],
		[
			"words"
		]
	],
	null
]
-- End --


Passing an uncompilable regexp throws an exception.

-- Testcase --
{%
	try {
		// unterminated capture group to trigger syntax error
		regexp("foo(");
	}
	catch (e) {
		// Massage compile error message for stable output since it is
		// dependant on the underyling C library.
		e.message = "Compile error";
		die(e);
	}
%}
-- End --

-- Expect stderr --
Compile error
In line 10, byte 8:

 `        die(e);`
  Near here ---^


-- End --


Passing an invalid flags argument throws an exception.

-- Testcase --
{%
	regexp(".*", true);
%}
-- End --

-- Expect stderr --
Type error: Given flags argument is not a string
In line 2, byte 19:

 `    regexp(".*", true);`
  Near here -----------^


-- End --

-- Testcase --
{%
	regexp(".*", "igz");
%}
-- End --

-- Expect stderr --
Type error: Unrecognized flag character 'z'
In line 2, byte 20:

 `    regexp(".*", "igz");`
  Near here ------------^


-- End --