gpt4 book ai didi

ruby - 正则表达式 - 分隔符之间的所有子字符串

转载 作者:数据小太阳 更新时间:2023-10-29 07:24:04 26 4
gpt4 key购买 nike

简单正则表达式的小问题...我有一个输入,需要两个单词之间的文本。输入示例:

Blah Blah 
Word1
New line text I need
Another important sentence for me
Word2
Blah blah
Word1
Line of important text
Word2
The end

我需要 Word1 和 Word2 之间的所有文本。有什么提示吗?

最佳答案

您可以使用正则表达式的先行和后行功能:

str = <<HERE
Blah Blah
Word1
New line text I need
Another important sentence for me
Word2
Blah blah
Word1
Line of important text
Word2
The end
HERE

str.scan(/(?<=Word1).+?(?=Word2)/m) # => ["\nNew line text I need\nAnother important sentence for me\n", "\nLine of important text\n"]

关于ruby - 正则表达式 - 分隔符之间的所有子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324579/

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