I am having trouble splitting a character from a string using a regex if there is a match.
I want to separate either the character "m" or "f" from the first part of the string, assuming that the next character is one or more numbers, followed by optional whitespace characters, followed by a string from the array that I have.
I tried:
2.4.0 :006 > MY_SEPARATOR_TOKENS = ["-", " to "] => ["-", " to "] 2.4.0 :008 > str = "M14-19" => "M14-19" 2.4.0 :011 > str.split(/^(m|f)\d+[[:space:]]*#{Regexp.union(MY_SEPARATOR_TOKENS)}/i) => ["", "M", "19"]
Notice the extraneous element "at the beginning of my array, and also note that the last expression is" 19 ", while I want everything else in the string (" 14-19 ").
How do I set up my regex so that only parts of the expression that get split get into the array?
source share