Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby regex for a split every four characters not working

Tags:

regex

ruby

I'm trying to split a sizeable string every four characters. This is how I'm trying to do it:

big_string.split(/..../) 

This is yielding a nil array. As far as I can see, this should be working. It even does when I plug it into an online ruby regex test.

like image 833
deeb Avatar asked Oct 27 '09 03:10

deeb


People also ask

What does =~ mean in Ruby regex?

=~ is Ruby's pattern-matching operator. It matches a regular expression on the left to a string on the right. If a match is found, the index of first match in string is returned. If the string cannot be found, nil will be returned.

How does split work 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.

How does split regex work?

Split method, except that Regex. Split splits the string at a delimiter determined by a regular expression instead of a set of characters. The input string is split as many times as possible. If pattern is not found in the input string, the return value contains one element whose value is the original input string.

How split a string in regex?

To split a string by a regular expression, pass a regex as a parameter to the split() method, e.g. str. split(/[,. \s]/) . The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.


2 Answers

Try scan instead:

$ irb >> "abcd1234beefcake".scan(/..../) => ["abcd", "1234", "beef", "cake"] 

or

>> "abcd1234beefcake".scan(/.{4}/) => ["abcd", "1234", "beef", "cake"] 

If the number of characters isn't divisible by 4, you can also grab the remaining characters:

>> "abcd1234beefcakexyz".scan(/.{1,4}/) => ["abcd", "1234", "beef", "cake", "xyz"] 

(The {1,4} will greedily grab between 1 and 4 characters)

like image 125
Ryan McGeary Avatar answered Oct 14 '22 10:10

Ryan McGeary


Hmm, I don't know what Rubular is doing there and why - but

big_string.split(/..../) 

does translate into

split the string at every 4-character-sequence

which should correctly result into something like

["", "", "", "abc"] 
like image 26
Marcel Jackwerth Avatar answered Oct 14 '22 10:10

Marcel Jackwerth