What's the difference between scan and match on Ruby string

后端 未结 3 1323
北荒
北荒 2020-12-02 16:40

I am new to Ruby and has always used String.scan to search for the first occurrence of a number. It is kind of strange that the returned value is in nested arra

3条回答
  •  感动是毒
    2020-12-02 17:39

    #scan returns everything that the Regex matches.

    #match returns the first match as a MatchData object, which contains data held by special variables like $& (what was matched by the Regex; that's what's mapping to index 0), $1 (match 1), $2, et al.

提交回复
热议问题