ruby – 正则表达式 – 分隔符之间的所有子串
发布时间:2020-12-17 03:53:23 所属栏目:百科 来源:网络整理
导读:容易正则表达式的小问题…我有一个输入,需要2个单词之间的文本. 输入示例: 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之间的所有文本.任何
容易正则表达式的小问题…我有一个输入,需要2个单词之间的文本.
输入示例: 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 neednAnother important sentence for men","nLine of important textn"] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |