[英]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
類和其他谷歌搜索的任何示例
一種解決方案是使用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
相反:返回與正則表達式不匹配的子字符串。
就像是
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.