diff --git a/vlib/regex/regex.v b/vlib/regex/regex.v index ea939d2d29..2d0d2d7400 100644 --- a/vlib/regex/regex.v +++ b/vlib/regex/regex.v @@ -2516,8 +2516,13 @@ pub fn (mut re RE) match_base(in_txt &u8, in_txt_len int) (int, int) { // println("ist_quant_n no_match_found") result = regex.no_match_found m_state = .stop + + // 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 + } continue - // return no_match_found, 0 } // ist_quant_p => quantifier positive test on token else if m_state == .ist_quant_p { diff --git a/vlib/regex/regex_complex_test.v b/vlib/regex/regex_complex_test.v new file mode 100644 index 0000000000..22edd0cf9c --- /dev/null +++ b/vlib/regex/regex_complex_test.v @@ -0,0 +1,19 @@ +import regex + +fn test_complex_matching() { + tmp_text := r"[export:'something'] +const ( +some_const = [1114111, 127, 2047, 65535, 1114111]! + +) + +[export:'something_empty'] +const ( +empty_const = () +) +" + query := r'\[export:\S+\]\sconst\s\(\s+\S+\s{3}=\s\(' + mut re := regex.regex_opt(query) or { panic(err) } + mut a := re.find_all(tmp_text) + assert a == [86, 138] +}