基于模式匹配从数组生成包含元素的字符串

时间:2021-03-05 20:05:45

I have an array like this:

我有一个像这样的数组:

inList = ["edge_rabbit", "nsp_edge_rabbit", "services", "syslog", "master_rabbit", "mongod", ...]

How can I take the elements that end with _rabbit or elements with the format mongo[cds], and generate a comma separated string like the following?

如何使用_rabbit结尾的元素或格式为mongo [cds]的元素,并生成逗号分隔的字符串,如下所示?

"edge_rabbit, nsp_edge_rabbit, master_rabbit"

2 个解决方案

#1


2  

Is this the one you are looking for using String#end_with? and Array#select?

这是你正在寻找使用String#end_with的那个吗?和数组#select?

inList = ["edge_rabbit", "nsp_edge_rabbit", "services", "syslog", "master_rabbit", "mongod"]
inList.select{|e| e.end_with?('_rabbit')}.join(", ")
# => "edge_rabbit, nsp_edge_rabbit, master_rabbit"

or

要么

inList = ["edge_rabbit", "nsp_edge_rabbit", "services", "syslog", "master_rabbit", "mongod"]
inList.grep(/_rabbit$/).join(", ")
# => "edge_rabbit, nsp_edge_rabbit, master_rabbit"


inList = ["edge_rabbit","_rabbit_ut", "nsp_edge_rabbit", "services", "syslog", "master_rabbit", "mongod","mongos","mongoy"]
inList.grep(/_rabbit$|^mongo[cds]/).join(", ")
# => "edge_rabbit, nsp_edge_rabbit, master_rabbit, mongod, mongos"

#2


1  

I'd use some small patterns:

我会用一些小图案:

in_list = ["edge_rabbit", "nsp_edge_rabbit", "services", "syslog", "master_rabbit", "mongod"]

in_list.select{ |s| s[/(?:^mongo)|(?:_rabbit$)/] }.join(', ') # => "edge_rabbit, nsp_edge_rabbit, master_rabbit, mongod"

Or:

要么:

in_list.grep(/(?:^mongo)|(?:_rabbit$)/).join(', ') # => "edge_rabbit, nsp_edge_rabbit, master_rabbit, mongod"

If it's possible to have variations on mongo with other trailing characters besides c, d or s, then use:

如果mongo可能与c,d或s之外的其他尾随字符有变化,那么使用:

in_list.grep(/(?:^mongo[cds])|(?:_rabbit$)/).join(', ') # => "edge_rabbit, nsp_edge_rabbit, master_rabbit, mongod"

#1


2  

Is this the one you are looking for using String#end_with? and Array#select?

这是你正在寻找使用String#end_with的那个吗?和数组#select?

inList = ["edge_rabbit", "nsp_edge_rabbit", "services", "syslog", "master_rabbit", "mongod"]
inList.select{|e| e.end_with?('_rabbit')}.join(", ")
# => "edge_rabbit, nsp_edge_rabbit, master_rabbit"

or

要么

inList = ["edge_rabbit", "nsp_edge_rabbit", "services", "syslog", "master_rabbit", "mongod"]
inList.grep(/_rabbit$/).join(", ")
# => "edge_rabbit, nsp_edge_rabbit, master_rabbit"


inList = ["edge_rabbit","_rabbit_ut", "nsp_edge_rabbit", "services", "syslog", "master_rabbit", "mongod","mongos","mongoy"]
inList.grep(/_rabbit$|^mongo[cds]/).join(", ")
# => "edge_rabbit, nsp_edge_rabbit, master_rabbit, mongod, mongos"

#2


1  

I'd use some small patterns:

我会用一些小图案:

in_list = ["edge_rabbit", "nsp_edge_rabbit", "services", "syslog", "master_rabbit", "mongod"]

in_list.select{ |s| s[/(?:^mongo)|(?:_rabbit$)/] }.join(', ') # => "edge_rabbit, nsp_edge_rabbit, master_rabbit, mongod"

Or:

要么:

in_list.grep(/(?:^mongo)|(?:_rabbit$)/).join(', ') # => "edge_rabbit, nsp_edge_rabbit, master_rabbit, mongod"

If it's possible to have variations on mongo with other trailing characters besides c, d or s, then use:

如果mongo可能与c,d或s之外的其他尾随字符有变化,那么使用:

in_list.grep(/(?:^mongo[cds])|(?:_rabbit$)/).join(', ') # => "edge_rabbit, nsp_edge_rabbit, master_rabbit, mongod"