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

  • SEARCH
  • Home
  • 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 6554905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:45:21+00:00 2026-05-25T12:45:21+00:00

Assuming a target string that’s on one hand arbitrary but, on the other hand,

  • 0

Assuming a target string that’s on one hand arbitrary but, on the other hand, guaranteed to contain a single decimal number (1 or more digits), I came up with the following regular regex pattern:

.*?(\d+).*?

So, if the target string is “(this is number 200)”, for example, Matcher.group(1) will contain the number.

Is there a more optimal regex pattern (or non-regex method) to extract this number?

By “optimal” I mean fastest (possibly with the least amount of CPU cycles). Java only.

  • 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-25T12:45:21+00:00Added an answer on May 25, 2026 at 12:45 pm

    I am sure regex and parseInt will perform well enough for you. However for your interest, I have compared it with a simple loop.

    public static final Pattern DIGITS = Pattern.compile("(\\d+)");
    
    public static void main(String[] args) {
      String text = "Some text before a number 123456 and some after";
      for (int i = 0; i < 5; i++) {
        timeRegex(text);
        timeLooping(text);
      }
    }
    
    private static int timeLooping(String text) {
      int ret = 0;
      final int runs = 1000;
      long start = System.nanoTime();
      for (int r = 0; r < runs; r++) {
        for (int i = 0; i < text.length(); i++) {
          char ch = text.charAt(i);
          if (ch <= '9' && ch >= '0')
            ret = ret * 10 + ch - '0';
          else if (ret > 0)
            break;
        }
      }
      long time = System.nanoTime() - start;
      System.out.printf("Took %,d ns to use a loop on average%n", time / runs);
      return ret;
    }
    
    private static int timeRegex(String text) {
      int ret = 0;
      final int runs = 1000;
      long start = System.nanoTime();
      for (int r = 0; r < runs; r++) {
        Matcher m = DIGITS.matcher(text);
        if (m.find())
          ret = Integer.parseInt(m.group());
      }
      long time = System.nanoTime() - start;
      System.out.printf("Took %,d ns to use a matcher on average%n", time / runs);
      return ret;
    }
    

    prints

    Took 19,803 ns to use a matcher on average
    Took 85 ns to use a loop on average
    Took 12,411 ns to use a matcher on average
    Took 83 ns to use a loop on average
    Took 8,199 ns to use a matcher on average
    Took 79 ns to use a loop on average
    Took 11,156 ns to use a matcher on average
    Took 104 ns to use a loop on average
    Took 4,527 ns to use a matcher on average
    Took 94 ns to use a loop on average
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have only the class name of a generic as a string in
Assuming String a and b: a += b a = a.concat(b) Under the hood,
Assuming a fluid layout is not an option (since that is a different discussion
I have a main thread that contains my WPF GUI and one or more
Problem: When dealing with web-applications that require grid-based input (but is not a fullblown
I have a method that takes a callback argument to execute asynchronously, but the
I've noticed that browsers don't recognize my password field as a potential auto-complete target.
1. Assuming that you don't have Visual Studio installed and you want to use
I'm building a thermostat that pulls its target temperatures from a Google Calendar. In
Assuming such a query exists, I would greatly appreciate the help. I'm trying to

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.