gpt4 book ai didi

Ruby - 匹配字符串中的所有模式

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:23 24 4
gpt4 key购买 nike

<分区>

需要帮助来匹配字符串中的所有模式。

s1 = " countries like US, Japan, Korea, Vietnam, Germany"
s2 = " countries like Holland, China, Korea, Thailand, UK"

从上面的字符串需要匹配特定的亚洲国家

regex = /(Japan)|(Korea)|(Vietnam)|(Cambodia)|(China)|(Thailand)/


irb(main):157:0> s1.match(regex)
=> #<MatchData "Japan" 1:"Japan" 2:nil 3:nil 4:nil>

有没有办法得到下面的输出?

s1.some_regex_operation # => Japan, Korea, Vietnam
s2.some_regex_operation # => China, Korea, Thailand

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com