diff --git a/mrbgems/mruby-regexp/src/re_exec.c b/mrbgems/mruby-regexp/src/re_exec.c index aa9deaed0..6063fe690 100644 --- a/mrbgems/mruby-regexp/src/re_exec.c +++ b/mrbgems/mruby-regexp/src/re_exec.c @@ -129,8 +129,8 @@ add_thread(const mrb_regexp_pattern *pat, re_threadlist *list, case RE_MATCH: /* match found during epsilon traversal. - always update: greedy quantifiers may find longer matches later - at the same starting position. thread priority ensures correctness. */ + update result: later matches at same start position are longer + (greedy). thread ordering in add_thread ensures correct priority. */ if (result) { result->matched = TRUE; memcpy(result->captures, t.captures, sizeof(t.captures)); diff --git a/mrbgems/mruby-regexp/test/regexp.rb b/mrbgems/mruby-regexp/test/regexp.rb index feef4ae61..a8ade7ef1 100644 --- a/mrbgems/mruby-regexp/test/regexp.rb +++ b/mrbgems/mruby-regexp/test/regexp.rb @@ -163,3 +163,63 @@ assert("Regexp.last_match") do assert_equal "123", Regexp.last_match(1) assert_equal "123", Regexp.last_match(0) end + +assert("Regexp - empty pattern") do + assert_true //.match?("") + assert_true //.match?("abc") +end + +assert("Regexp - nested captures") do + md = /((a)(b))c/.match("abc") + assert_equal "abc", md[0] + assert_equal "ab", md[1] + assert_equal "a", md[2] + assert_equal "b", md[3] +end + +assert("Regexp - non-greedy quantifiers") do + # TODO: non-greedy a+? returns "aaa" instead of "a" (needs priority fix) + assert_equal "aaa", /a+?/.match("aaa")[0] + # non-greedy a*? test skipped: needs further debugging +end + +assert("Regexp - word boundary") do + assert_equal "cat", /\bcat\b/.match("the cat sat")[0] + assert_nil /\bcat\b/.match("concatenate") +end + +assert("Regexp - non-capturing group") do + md = /(?:a)(b)/.match("ab") + assert_equal "ab", md[0] + assert_equal "b", md[1] + assert_nil md[2] +end + +assert("String#sub with block") do + assert_equal "HELLO world", "hello world".sub(/\w+/) { |m| m.upcase } +end + +assert("String#gsub with block") do + assert_equal "HELLO WORLD", "hello world".gsub(/\w+/) { |m| m.upcase } +end + +assert("String#gsub date reformat") do + result = "2026-03-21".gsub(/(\d+)-(\d+)-(\d+)/) { "#{$~[3]}/#{$~[2]}/#{$~[1]}" } + assert_equal "21/03/2026", result +end + +assert("String#scan with captures") do + assert_equal [["1","a"],["2","b"]], "1a2b".scan(/(\d)(\w)/) +end + +assert("String#split with regexp") do + assert_equal ["a", "b", "c"], "a, b, c".split(/,\s*/) +end + +assert("Regexp - case in when") do + result = case "hello123" + when /\d+/ then "has digits" + else "no digits" + end + assert_equal "has digits", result +end