Ruby正则表达式提取{{word | word | word} {word | word | word}}之间的单词

时间:2014-04-29 10:22:15

标签: ruby regex

如何才能将{}中包含的单个单词从文本

中删除
an example of the text {Creating|Making|Producing} blah blah blah

another example of the text {{In order to|As a way to|So that you can|To be able to} {connect|link|join}} blah blah blah

凭借我有限的正则表达式知识,我已经掌握了这一点

text.scan(/{([^}]*)}/)[0][0].split('|')

但是这会选择文字{In order to|As a way to|So that you can|To be able to并将其拆分为{In order to

如何仅在{}

中选择文字

预期输出

["In order to", "As a way to", "So that you can", "To be able to"]

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试:

text.scan(/{([^{}]*)}/)[0][0].split('|')