I'd like to do grouping of regular expressions with eiffel. How do I do something like
l_reg.compile ("^([0-9]{3}) (rabbit[0-9]).*")
l_groups := l_reg.groups ("123 rabbit1")
my_first_rabbit := l_groups.at (2)
Didn't find any example on groups, LX_DFA_REGULAR_EXPRESSION
class and other googlings
One solution is to use RX_PCRE_REGULAR_EXPRESSION
instead of LX_DFA_REGULAR_EXPRESSION
:
Including $ISE_LIBRARY\contrib\library\gobo\library\regexp\src\library.ecf
library
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)
There is no groups
routine, although it could be implemented by calling captured_substring
internally. There is only a routine split
which does the reverse: returns the substrings which did not match the regular expression.
Something like
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
you could test your regex here: https://www.regextester.com/1911