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 8759429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:48:10+00:00 2026-06-13T14:48:10+00:00

Using the code of a question I just answered as an example Starting with

  • 0

Using the code of a question I just answered as an example

Starting with the string

30-Nov-2012 30-Nov-2012 United Kingdom, 31-Oct-2012 31-Oct-2012 United
Arab Emirates, 29-Oct-2012 31-Oct-2012 India 

What if we wanted to replace the space after every four-digit number with an @ such that you end up with something like this:

30-Nov-2012@30-Nov-2012@United Kingdom, 31-Oct-2012@31-Oct-2012@United
Arab Emirates, 29-Oct-2012@31-Oct-2012@India  

How much more efficient is it to use a back-reference rather than a positive lookbehind (if at all)?

back-reference:

inputString.replaceAll("(\\d{4})\\s", "$1@");

positive lookbehind:

inputString.replaceAll("(?<=\\d{4})\\s", "@");
  • 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-06-13T14:48:12+00:00Added an answer on June 13, 2026 at 2:48 pm

    I admit my testing methodology is crude and may be flawed (furthermore I don’t know Java, only learning enough to write this answer), but my initial evidence proves contrary to @dasblinkenlight’s answer. I ran the following code:

    import java.util.*;
    import java.lang.*;
    
    class Main
    {
        private static void test (String regex, String replace, int repetitions)
        {
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < repetitions; i++)
            {
                String str = "30-Nov-2012 United Kingdom, 31-Oct-2012 31-Oct-2012 United Arab Emirates, 29-Oct-2012 31-Oct-2012 India, ";
                str.replaceAll(regex, replace);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("Execution time: " + Long.toString(endTime - startTime));
        }
    
        public static void main (String[] args) throws java.lang.Exception
        {
            test("(\\d{4})\\s", "$1@", 10000);
            test("(?<=\\d{4})\\s", "@", 10000);
            test("(\\d{4})\\s", "$1@", 10000);
            test("(?<=\\d{4})\\s", "@", 10000);
            test("(\\d{4})\\s", "$1@", 10000);
            test("(?<=\\d{4})\\s", "@", 10000);
            test("(\\d{4})\\s", "$1@", 10000);
            test("(?<=\\d{4})\\s", "@", 10000);
        }
    }
    

    …here, http://ideone.com/WkHLMN, and the output was:

    Execution time: 164
    Execution time: 140
    Execution time: 96
    Execution time: 135
    Execution time: 95
    Execution time: 133
    Execution time: 94
    Execution time: 130
    

    Ignoring the first set of cases as outliers having to do with initialization, the remainder cases seem to indicate that the latter expression, using the positive lookbehind assertion, might do as much as 50% more work! I suspect this may be the case because backreferences would require going back to check whether an assertion is true, after passing the characters of interest.

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

Sidebar

Related Questions

I'm trying to implement the example code of the following question by using opencv
I am using the code as described in this question. However get following error
Question: How can I document Ruby code using Doxygen? Disclaimer: I know ruby already
I found a very interesting question. When I'm using following code: int main() {
I am using the BlockingQueue code posted in this question , but realized I
I am using Polymorphic Models . Simple Question: My code below works without using
So, as per a separate question that I asked I'm using the following code
I am using the accepted answer from this question . The relevant code is
Simple enough question I hope. I am using the following type of code to
Stackoverflow is awesome! I just posted a question and it was answered by the

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.