aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/examples/regex/pcre.vv
blob: 72beaf553e1af96c325730a3b23bc3bebb6df77e (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
module main

// NB: you need to `v install pcre` to be able to compile this example.

import pcre

fn example() {
	r := pcre.new_regex('Match everything after this: (.+)', 0) or {
		println('An error occured!')
		return
	}

	m := r.match_str('Match everything after this: "I ❤️ VLang!"', 0, 0) or {
		println('No match!')
		return
	}

	// m.get(0) -> Match everything after this: "I ❤️ VLang!"
	// m.get(1) -> "I ❤️ VLang!"'
	// m.get(2) -> Error!
	whole_match := m.get(0) or {
		println('We matched nothing...')
		return
	}

	matched_str := m.get(1) or {
		println('We matched nothing...')
		return
	}

	println(whole_match) // Match everything after this: "I ❤️ VLang!"
	println(matched_str) // "I ❤️ VLang!"
}

fn main() {
	example()

	mut text := '[ an s. s! ]( wi4ki:something )
	[ an s. s! ]( wi4ki:something )
	[ an s. s! ](wiki:something)
	[ an s. s! ](something)dd
	d [ an s. s! ](something ) d
	[  more text ]( something ) s [ something b ](something)dd

	'

	// check the regex on https://regex101.com/r/HdYya8/1/

	regex := r'(\[[a-z\.\! ]*\]\( *\w*\:*\w* *\))*'

	r := pcre.new_regex(regex, 0) or {
		println('An error occured!')
		return
	}

	m := r.match_str(text, 0, 0) or {
		println('No match!')
		return
	}

	whole_match1 := m.get(0) or {
		println('We matched nothing 0...')
		return
	}

	println(whole_match1)

	println(m.get_all())
}