Split string by regex in Ruby - ruby

I need to split a string by commas that are outside brackets. I have this string:
'a,b,c,d[a,b,c[a,b]],e'
and my split needs to return:
['a', 'b', 'c', 'd[a,b,c[a,b]]', 'e']
How can I do that?

'a,b,c,d[a,b,c[a,b]],e'
.scan(/(?:\[[^\]]*\]|[^,])+/)
# => ["a", "b", "c", "d[a,b,c[a,b]]", "e"]
'a,[a][b],e'
.scan(/(?:\[[^\]]*\]|[^,])+/)
# => ["a", "[a][b]", "e"]

Related

How to split string with accented characters in ruby

Currently I got :
"mɑ̃ʒe".split('')
# => ["m", "ɑ", "̃", "ʒ", "e"]
I would like to get this result
"mɑ̃ʒe".split('')
# => ["m", "ã", "ʒ", "e"]
Use String#each_grapheme_cluster instead. For example:
"mɑ̃ʒe".each_grapheme_cluster.to_a
#=> ["m", "ɑ̃", "ʒ", "e"]

Get all overlapping matches of any length

I'm trying to achieve:
'abc'.scan(regex) #=> ['a', 'b', 'c', 'ab', 'bc', 'abc']
It can be done like this:
(1..'abc'.size).map {|l| 'abc'.scan /(?=(\w{#{l}}))/}.flatten
#=> ["a", "b", "c", "ab", "bc", "abc"]
But I would like to do it in one regex expression.
What about without regex?:
string = 'abc'
p (1..string.size).flat_map { |e| string.chars.each_cons(e).map(&:join) }
# ["a", "b", "c", "ab", "bc", "abc"]

How do I split on multiple conditions?

With Ruby how do I split on either one of tow conditions -- wheter there are 3 or more spaces or a tab charadter? I tried this
2.4.0 :003 > line = "a\tb\tc"
=> "a\tb\tc"
2.4.0 :004 > line.split(/([[:space:]][[:space:]][[:space:]]+|\t)/)
=> ["a", "\t", "b", "\t", "c"]
but as you can see, the tab character itself is getting included in my results. The results should be
["a", "b", "c"]
What about just split?
p "a\tb\tc".split
# ["a", "b", "c"]
p "a\tb\tc\t\tc\t\t\t\t\t\t\tc\ts\ts\tt".split
# ["a", "b", "c", "c", "c", "s", "s", "t"]
Although that doesn't split when there are three 3 or more white spaces, this might work:
p "a\tb\tc\t\tc\t\t\ t\t\tc\ts\ts\tt".split(/\s{3,}|\t/)
# => ["a", "b", "c", "c", "t", "c", "s", "s", "t"]
line = "aa bb cc\tdd"
line.split /\p{Space}{3,}|\t+/
#⇒ ["aa bb", "cc", "dd"]

Splitting a string into an array ruby

I am trying to turn this string
"a,bc,c"
into this array..
["a", "b", "c"]
I've used split on the comma & iterated through it but I'd like to find a cleaner way.
Thanks!
I will use #scan and #uniq method.
"a, bc,c".scan(/[a-z]/).uniq
# => ["a", "b", "c"]
Here we go, one option:
"a, bc,c".gsub(/\W+/, '').chars.uniq
# Outputs:
=> ["a", "b", "c"]

Array entries from string don't map to the hash

I am very new to Ruby, and programming in general. Firstly, I have the below code:
hashy = {"a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 6, "f" => 6}
array = ["a", "b", "c"]
string = "df"
array.push (string.split(//))
puts array
test = array.map {|a| hashy.select {|k,v| a == k}}
puts test
This code successfully maps 'a', 'b' and 'c' to the hash, and populates test with the keys and values from the hash.
This always works for a pre-defined array. However if I add to the array from a string (in this case the string "df", or create an array from a string, it no longer maps the array values to the hash, and I can't see why. I've looked at different ways of populating the array with the string values, but each time get the same problem.
As far as I can see "df" should also be mapping to the hash.
Any help would be greatly appreciated.
It's because you pushing string.split(//) array to array as one object, so you have one array element among the numbers in array as result.
array = ["a", "b", "c"]
string = "df"
array.push (string.split(//))
=> ["a", "b", "c", ["d", "f"]]
To avoid this, you can use array concatenation, for example
array = ["a", "b", "c"]
string = "df"
array += string.split(//)
=> ["a", "b", "c", "d", "f"]

Resources