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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -