Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6620869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:09:47+00:00 2026-05-25T21:09:47+00:00

This is a two part question. Given you have an array of strings that

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:09:47+00:00Added an answer on May 25, 2026 at 9:09 pm

    The easiest way to answer the first part is to, as always, benchmark it with your real data. For example:

    require 'benchmark'
    Benchmark.bm do |x|
      x.report { 50000.times { a = 'a@b.c'.split('@')[0] } }
      x.report { 50000.times { a = 'a@b.c'[/[^@]+/] } }
    end
    

    says (on my setup):

          user     system      total        real
      0.130000   0.010000   0.140000 (  0.130946)
      0.090000   0.000000   0.090000 (  0.096260)
    

    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 the split solution gets your desired result in a slightly roundabout way.

    The split approach 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:

    my_string[/[^.]+/]
    

    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)".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about a regex. Given this part of a regex: (.[^\\.]+)
This is a two part question, but first some background information: I have a
This is a two part question. A dumb technical query and a broader query
This is a two part question. Is it possible to take advantage of xVal
This is a two-part question: First, I am interested to know what the best
This is a two-part question. I'm using jQuery for a project and wanting to
This is actually a two part question. First,does the HttpContext.Current correspond to the current
This is kinda....a two part question. The first one is much more important than
This is kind of a two part question. But it both relates to the
This is a two part question - first I need to get every element

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.