我有一個組可以在輸入字符串中出現任意次數。我需要獲得所有匹配項目的列表。如何獲得一組正則表達式匹配列表?
例如,對於輸入:
example repeattext 1 anything here repeattext 2 anything repeattext 3
我的正則表達式是:
(repeattext \d)
我想對小組賽的名單。有可能在這裏使用正則表達式,還是我需要自己解析它?
我有一個組可以在輸入字符串中出現任意次數。我需要獲得所有匹配項目的列表。如何獲得一組正則表達式匹配列表?
例如,對於輸入:
example repeattext 1 anything here repeattext 2 anything repeattext 3
我的正則表達式是:
(repeattext \d)
我想對小組賽的名單。有可能在這裏使用正則表達式,還是我需要自己解析它?
是的,你可以在這裏使用正則表達式。你現有的正則表達式會很好。
請參閱http://rubular.com/r/fS8c9C61rG在您的示例中使用它。
如果數字會不會成爲10或更高,應考慮此正則表達式:
(repeattext \d+)
^
|
`- matches 1 or more repeating of previous
使用
result = subject.scan(/repeattext \d+/)
=> ["repeattext 1", "repeattext 2", "repeattext 3"]
你能解釋一下「group」是什麼意思,它與「regex」有什麼不同,就像你看到的那樣?你能否展示一個完整的例子,包括你使用的語言,以及你爲了匹配字符串而編寫的代碼? – Kobi