This is a two part question. Given you have an array of strings that can be split at a character (e.g. email addresses at ‘@’ or file names at ‘.’) which is the the most performant way of finding the characters before the split character?
my_string.split(char)[0]
or
my_string[/regex/]
The second part of the question is what’s how do you write a regex to get everything before the first instance of a character. The regex below finds certain characters before a ‘.’ (because ‘.’ is not in the pattern) but that was my hacky way to get to a solution.
my_string[/[A-Za-z0-9\_-]+/]
thanks!
The easiest way to answer the first part is to, as always, benchmark it with your real data. For example:
says (on my setup):
So the regex solution looks a little bit faster but the difference is barely noticeable even with 50 000 iterations. OTOH, the regex solution says exactly what you mean ("give me everything before the first
@") whereas thesplitsolution gets your desired result in a slightly roundabout way.The
splitapproach is probably slower because it has to scan the whole string to split it into pieces, then build an array of the pieces, and finally extract the first element of the array and throw the rest away; I don’t know if the VM is clever enough to recognize that it doesn’t need to build the array so that’s just a bit of quick guess work.As far as your second question is concerned, say what you mean:
If you want everything before the first period then say "everything until a period" rather than "the first chunk that is made of these characters (which happen to not contain a period)".