From b03ee59fd4656b1d1cb443cc9b5bd362f55c4e29 Mon Sep 17 00:00:00 2001 From: Christian Ferbar <5595808+ferbar@users.noreply.github.com> Date: Tue, 21 Jul 2020 11:31:51 +0200 Subject: [PATCH] MatchPattern: allow regex patterns (#3071) * MatchPattern: allow regex patterns Signed-off-by: Christian Ferbar <5595808+ferbar@users.noreply.github.com> Co-authored-by: Christian Ferbar --- lib/fluent/match.rb | 9 +++++++++ test/test_match.rb | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/fluent/match.rb b/lib/fluent/match.rb index 2f3e7eb4e6..17122e07ef 100644 --- a/lib/fluent/match.rb +++ b/lib/fluent/match.rb @@ -33,6 +33,15 @@ def match(str) class GlobMatchPattern < MatchPattern def initialize(pat) + if pat.start_with?('/') + if pat.end_with?('/') + @regex = Regexp.new("\\A"+pat[1..-2]+"\\Z") + return + else + raise Fluent::ConfigError, "invalid match - regex" + end + end + stack = [] regex = [''] escape = false diff --git a/test/test_match.rb b/test/test_match.rb index 1dcf3c80eb..4272a5c8c3 100644 --- a/test/test_match.rb +++ b/test/test_match.rb @@ -101,6 +101,17 @@ def test_multi_pattern_or assert_or_not_match('a.b.** a.c', 'a.c.d') end + def test_regex_pattern + assert_glob_match('/a/', 'a') + assert_glob_not_match('/a/', 'abc') + assert_glob_match('/a.*/', 'abc') + assert_glob_not_match('/b.*/', 'abc') + assert_glob_match('/a\..*/', 'a.b.c') + assert_glob_not_match('/(?!a\.).*/', 'a.b.c') + assert_glob_not_match('/a\..*/', 'b.b.c') + assert_glob_match('/(?!a\.).*/', 'b.b.c') + end + #def test_character_class # assert_match('[a]', 'a') # assert_match('[ab]', 'a')