v / vlib / regex / regex_complex_test.v
54 lines · 45 sloc · 1.28 KB · 757929392e0e7a75fc1272116460981e589737d5
Raw
1import regex
2
3fn test_complex_matching001() {
4 tmp_text := r"@[export:'something']
5const (
6some_const = [1114111, 127, 2047, 65535, 1114111]!
7
8)
9
10@[export:'something_empty']
11const (
12empty_const = ()
13)
14"
15 query := r'\[export:\S+\]\sconst\s\(\s+\S+\s{3}=\s\('
16 mut re := regex.regex_opt(query) or { panic(err) }
17 mut a := re.find_all(tmp_text)
18 assert a == [88, 140]
19}
20
21fn test_complex_matching002() {
22 text := '<?xml version="1.0" encoding="utf-8"?>
23<resources>
24 <string name="v_app_name">V0</string>
25 <!-- comment -->
26 <string name="v_lib_name">v1</string>
27 <string name="v_lib_name2">v2</string>
28</resources>
29'
30
31 mut re := regex.regex_opt(r'.*<resources>.+<string name="v_lib_name">([^<]+)') or { panic(err) }
32
33 start, end := re.match_string(text)
34 if start >= 0 && re.groups.len > 0 {
35 // check that we have obtained our 'v1' value
36 assert text#[re.groups[0]..re.groups[1]] == 'v1'
37 return
38 }
39 assert false
40}
41
42fn test_complex_matching003() {
43 text := 'abcdefgt1234abcd<tag name="mio_tag">value</tag>'
44
45 mut re := regex.regex_opt(r'\w*<tag\s*name\s*="mio_tag">([^<]+)') or { panic(err) }
46
47 start, end := re.match_string(text)
48 if start >= 0 && re.groups.len > 0 {
49 println('found ${text#[start..end]}')
50 println('group: ${text#[re.groups[0]..re.groups[1]]}')
51 return
52 }
53 assert false
54}
55