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

timeout - Handshake_timeout on RabbitMQ using python and pika from remote vm -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

c# - Search and Add Comment with OpenXML for Word -