简单的正则表达式问题.我有一个以下格式的字符串:
this is a [sample] string with [some] special words. [another one]
Run Code Online (Sandbox Code Playgroud)
提取方括号内的单词的正则表达式是什么,即.
sample
some
another one
Run Code Online (Sandbox Code Playgroud)
注意:在我的用例中,括号不能嵌套.
我需要MatchData在字符串中每次出现正则表达式.这与Regex的Match All Occurrences中建议的扫描方法不同,因为它只给我一个字符串数组(我需要完整的MatchData,以获取开始和结束信息等).
input = "abc12def34ghijklmno567pqrs"
numbers = /\d+/
numbers.match input # #<MatchData "12"> (only the first match)
input.scan numbers # ["12", "34", "567"] (all matches, but only the strings)
Run Code Online (Sandbox Code Playgroud)
我怀疑有一些方法我忽略了.建议?
我有以下正则表达式,它应该匹配所有带有括号的单词(包括括号),但它只匹配一个case.我究竟做错了什么?
"(e), (f), and (g)".match(/\(\w+\)/)
=> #<MatchData "(e)">
Run Code Online (Sandbox Code Playgroud)
输出应该是:
=> #<MatchData "(e)", 1: "(f)", 2: "(g)">
Run Code Online (Sandbox Code Playgroud)