Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting with empty space in Ruby [duplicate]

Tags:

regex

ruby

In both Ruby and JavaScript I can write expression " x ".split(/[ ]+/) . In JavaScript I get somehow reasonable result ["", "x", ""], but in Ruby (2.0.0) i get ["", "x"], which is for me quite counterintuitive. I have problems to understand how regular expressions works in Ruby. Why don't I get the same result as in JavaScript or just ["x"]?

like image 497
Ondra Avatar asked Mar 11 '14 11:03

Ondra


People also ask

How to separate a string on spaces in Ruby?

In Ruby, a string, or a regular expression, is used as the separator. Split details. This method is widely used. When we omit an argument, it separates a string on spaces.

How to split a string into an array in Ruby?

split is a String class method in Ruby which is used to split the given string into an array of substrings based on a pattern specified. Here the pattern can be a Regular Expression or a string. If pattern is a Regular Expression or a string, str is divided where the pattern matches. Syntax: arr = str.split(pattern, limit) public

How do you split a string with no empty values?

So The resulting string array has no empty values. It contains just the four words stored within the text. value = "one, two three: four" # Split on one or more non-word characters. a = value.split ( /\W+/ ) # Display result. puts a one two three four \W+ One or more non-word characters. Limit.

How to make an array with no empty values in Ruby?

In Ruby we specify these with forward slashes. Here We specify a delimiter of one or more non-word characters. The Kleene closure + indicates "one or more." Tip The delimiter here matches one or two characters. It includes both the comma and the following space. So The resulting string array has no empty values.


2 Answers

From string#split documentation, emphasis my own:

split(pattern=$;, [limit])

If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored.

If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern contains groups, the respective matches will be returned in the array as well.

If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ` ' were specified.

If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.

So if you were to use " x ".split(/[ ]+/, -1) you would get your expected result of ["", "x", ""]

*edited to reflect Wayne's comment

like image 166
dax Avatar answered Oct 19 '22 10:10

dax


I found this in the C code for String#split, almost right at the end:

if (NIL_P(limit) && lim == 0) {
long len;
while ((len = RARRAY_LEN(result)) > 0 &&
       (tmp = RARRAY_AREF(result, len-1), RSTRING_LEN(tmp) == 0))
    rb_ary_pop(result);
}

So it actually pops empty strings off the end of the result array before returning! It looks like the creators of Ruby didn't want String#split to return a bunch of empty strings.

Notice the check for NIL_P(limit) -- this accords exactly with what the documentation says, as @dax pointed out.

like image 24
Alex D Avatar answered Oct 19 '22 09:10

Alex D