1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

regex: fix issue with multiple repetitions out of the groups (#17774)

This commit is contained in:
penguindark 2023-03-25 19:49:01 +01:00 committed by GitHub
parent 24cc5920e6
commit 3d2d330478
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 2 deletions

View File

@ -2520,7 +2520,9 @@ pub fn (mut re RE) match_base(in_txt &u8, in_txt_len int) (int, int) {
// stop already started matching outside a capturing group
if re.state_list.len > 0 && re.state_list.last().group_index == -1
&& re.state_list.last().last_dot_pc > 0 {
return regex.no_match_found, 0
if ist == regex.ist_dot_char || ist == regex.ist_bsls_char {
return regex.no_match_found, 0
}
}
continue
}

View File

@ -1,6 +1,6 @@
import regex
fn test_complex_matching() {
fn test_complex_matching001() {
tmp_text := r"[export:'something']
const (
some_const = [1114111, 127, 2047, 65535, 1114111]!
@ -17,3 +17,38 @@ empty_const = ()
mut a := re.find_all(tmp_text)
assert a == [86, 138]
}
fn test_complex_matching002() {
text := '<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="v_app_name">V0</string>
<!-- comment -->
<string name="v_lib_name">v1</string>
<string name="v_lib_name2">v2</string>
</resources>
'
mut re := regex.regex_opt(r'.*<resources>.+<string name="v_lib_name">([^<]+)') or { panic(err) }
start, end := re.match_string(text)
if start >= 0 && re.groups.len > 0 {
// check that we have obtained our 'v1' value
assert text#[re.groups[0]..re.groups[1]] == 'v1'
return
}
assert false
}
fn test_complex_matching003() {
text := 'abcdefgt1234abcd<tag name="mio_tag">value</tag>'
mut re := regex.regex_opt(r'\w*<tag\s*name\s*="mio_tag">([^<]+)') or { panic(err) }
start, end := re.match_string(text)
if start >= 0 && re.groups.len > 0 {
println('found ${text#[start..end]}')
println('group: ${text#[re.groups[0]..re.groups[1]]}')
return
}
assert false
}