Regex Negative Lookbehind is your friend in cases like this:
iex(24)> String.split("abcde", ~r/((?<!\s)b(?!\s)|\s)/)
["a", "cde"]
iex(25)> String.split("a b cde", ~r/((?<!\s)b(?!\s)|\s)/)
["a", "b", "cde"]
iex(26)> String.split("ab cde", ~r/((?<!\s)b(?!\s)|\s)/)
["ab", "cde"]
iex(27)> String.split("a bcde", ~r/((?<!\s)b(?!\s)|\s)/)
["a", "bcde"]
The above pattern will match a b for splitting, only if it’s entirely surrounded by non-whitespace, eg. it’s not preceded by whitespace ((?<!\s)) and it’s not followed by whitespace ((?!\s)). It’ll also split on standalone whitespace ((...|\s)).
Depending on your exact needs you may want to adjust the pattern, of course.


















