Eiffel:正则表达式如何做分组

Eiffel: regular expressions how to do grouping

我想用 eiffel 对正则表达式进行分组。我该怎么做

l_reg.compile ("^([0-9]{3}) (rabbit[0-9]).*")
l_groups := l_reg.groups ("123 rabbit1")
my_first_rabbit := l_groups.at (2)

未在群组、LX_DFA_REGULAR_EXPRESSION class 和其他谷歌搜索中找到任何示例

一个解决方案是使用 RX_PCRE_REGULAR_EXPRESSION 而不是 LX_DFA_REGULAR_EXPRESSION:

包括$ISE_LIBRARY\contrib\library\gobo\library\regexp\src\library.ecf图书馆

l_reg: RX_PCRE_REGULAR_EXPRESSION
...
l_reg.compile ("^([0-9]{3}) (rabbit[0-9]).*")
l_reg.match ("123 rabbit1")
my_first_rabbit := l_reg.captured_substring (2)

没有groups例程,虽然可以通过内部调用captured_substring实现。只有一个例程 split 执行相反的操作:returns 与正则表达式不匹配的子字符串。

类似

    regex_groups (a_haystack, a_needle: STRING): ARRAY[STRING]
            -- Test with https://www.regextester.com/1911
        require
            regex_match (a_haystack, a_needle)
        local
            l_reg: RX_PCRE_REGULAR_EXPRESSION
            l_index: like {RX_PCRE_REGULAR_EXPRESSION}.match_count
        do
            create Result.make_empty
            create l_reg.make
            l_reg.compile (a_needle)
            if l_reg.is_compiled then
                l_reg.match (a_haystack)
                from
                    l_index := 1
                until
                    l_index > l_reg.match_count
                loop
                    Result.extend (l_reg.captured_substring (l_index))
                    l_index := l_index + 1
                end
            else
                --- error compiling regex
                fallible.set_last_error (create {SIT_INTERNAL_ERROR}.make ("regex_group-> regex does not compile:" + a_needle))
            end
        ensure
            not fallible.has_error
            instance_free: Class
        end

你可以在这里测试你的正则表达式:https://www.regextester.com/1911