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.
=~ 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.
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.
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.
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.
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)
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With