gpt4 book ai didi

regex - 如何使用正则表达式在字符串中查找模式的所有匹配项

转载 作者:行者123 更新时间:2023-12-04 00:01:47 25 4
gpt4 key购买 nike

如果我有一个字符串:

s = "This is a simple string 234 something else here as well 4334

和一个正则表达式,如:
regex = ~"[0-9]{3}"

如何使用该正则表达式从字符串中提取所有单词?在这种情况下 234433 ?

最佳答案

您可以使用 CharSequence.findAll :

def triads = s.findAll("[0-9]{3}")

assert triads == ['234', '433']

Latest documentation of CharSequence.findAll

关于regex - 如何使用正则表达式在字符串中查找模式的所有匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25558031/

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