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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:13:38+00:00 2026-06-15T21:13:38+00:00

Trying to efficiently extract some numbers from a string and have tried java.util.regex.Matcher com.google.common.base.Splitter

  • 0

Trying to efficiently extract some numbers from a string and have tried

  • java.util.regex.Matcher
  • com.google.common.base.Splitter

The results were :

  • via Regular Expression: 24417 ms
  • via Google Splitter: 17730 ms

Is there another faster way you can recommend ?

I know similar questions asked before e.g. How to extract multiple integers from a String in Java? but my emphasis is on making this fast (but maintainable/simple) as it happens a lot.


EDIT : Here are my final results which tie in with those from Andrea Ligios below:

  • Regular Expression (without brackets) : 18857
  • Google Splitter (without the superflous trimResults() method): 15329
  • Martijn Courteaux answer below: 4073

import org.junit.Test;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Sample {

    final static int COUNT = 50000000;
    public static final String INPUT = "FOO-1-9-BAR1"; // I want 1, 9, 1

    @Test
    public void extractNumbers() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
            // Output is list of 1, 9, 1
            Demo.extractNumbersViaGoogleSplitter(INPUT);
        }
        System.out.println("Total execution time (ms) via Google Splitter: " + (System.currentTimeMillis() - startTime));


        startTime = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
            // Output is list of 1, 9, 1
            Demo.extractNumbersViaRegEx(INPUT);
        }
        System.out.println("Total execution time (ms) Regular Expression: " + (System.currentTimeMillis() - startTime));

    }
}

class Demo {

    static List<Integer> extractNumbersViaGoogleSplitter(final String text) {

        Iterator<String> iter = Splitter.on(CharMatcher.JAVA_DIGIT.negate()).trimResults().omitEmptyStrings().split(text).iterator();
        final List<Integer> result = new ArrayList<Integer>();
        while (iter.hasNext()) {
            result.add(Integer.parseInt(iter.next()));

        }
        return result;
    }
    /**
     * Matches all the numbers in a string, as individual groups. e.g.
     * FOO-1-BAR1-1-12 matches 1,1,1,12.
     */
    private static final Pattern NUMBERS = Pattern.compile("(\\d+)");

    static List<Integer> extractNumbersViaRegEx(final String source) {
        final Matcher matcher = NUMBERS.matcher(source);
        final List<Integer> result = new ArrayList<Integer>();

        if (matcher.find()) {
            do {
                result.add(Integer.parseInt(matcher.group(0)));
            } while (matcher.find());
            return result;
        }
        return result;
    }
}
  • 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-15T21:13:39+00:00Added an answer on June 15, 2026 at 9:13 pm

    This is a very quick algorithm:

    public List<Integer> extractIntegers(String input)
    {
        List<Integer> result = new ArrayList<Integer>();
        int index = 0;
        int v = 0;
        int l = 0;
        while (index < input.length())
        {
            char c = input.charAt(index);
            if (Character.isDigit(c))
            {
                v *= 10;
                v += c - '0';
                l++;
            } else if (l > 0)
            {
                result.add(v);
                l = 0;
                v = 0;
            }
            index++;
        }
        if (l > 0)
        {
            result.add(v);
        }
        return result;
    }
    

    This code took on my machine 3672 milliseconds, for “FOO-1-9-BAR1” and 50000000 runs. I’m on a 2.3 GHz core.

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

Sidebar

Related Questions

i am getting started with regex in java and am trying to extract the
I am trying to extract a substring from a string in Tcl. I wrote
I'm trying to efficiently list numbers between 1 and 100. However I have to
We're trying to count number of ampersands in a string efficiently using Regex. I
I am trying to parse out some information from Google's geocoding API but I
I am trying to efficiently read from the stdin by using setvbuf in `_IOFBF~
I have a script to extract certain data from a much bigger table, with
I am trying to do three simple steps efficiently in Python. I have a
I'm trying to extract contact informations from android devices and synchronize it with the
I have a boost dynamic_bitset that I am trying to extract the set bits

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.