[英]How can I extract the words from this string “ !!one!! **two** @@three@@” using Regex in Ruby
在IRB中,我可以這樣做:
c = /(\\ b \\ w + \\ b)\\ W *(\\ b \\ w + \\ b)\\ W *(\\ b \\ w + \\ b)\\ W * / .match(“!! one ** * two * @ @ three @@“)
得到這個:
=> MatchData“一個** * 兩個 * @@三個@@”1:“一個”2:“兩個”3:“三個”
但假設我事先不知道單詞的數量,我怎樣才能從字符串中提取所有單詞“。例如,它可能是”!! ** ** * 2 * @@ three @@“例如,但在另一個例子中可能是“!! five ** * six *”。
謝謝。
> " !!one** *two* @@three@@ ".scan(/\w+/)
=> ["one", "two", "three"]
此外,在使用()
情況下, scan
可以返回數組數組。
> "Our fifty users left 500 posts this month.".scan(/([a-z]+|\d+)\s+(posts|users)/i)
=> [["fifty", "users"], ["500", "posts"]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.