ruby - Different return values of `String#split` method using regex -
why second split
in following return punctuation? why using parentheses in regular expression change output?
str = "this test string. let's split it." str.split(/\. /) # =>["this test string", "let's split it."] str.split(/(\. )/) # =>["this test string", ". ", "let's split it."]
because second code uses regex contains group. string#split
:
if pattern
regexp
,str
divided pattern matches. whenever pattern matches zero-length string, str split individual characters. if pattern contains groups, respective matches returned in array well.
Comments
Post a Comment