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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:43:12+00:00 2026-06-09T10:43:12+00:00

In Java, when we try to do pattern matching using a regular expression. e.g.

  • 0

In Java, when we try to do pattern matching using a regular expression. e.g. take a input string and use regular expression to find out if it is numeric. If not, throw an exception.
In this case, I understand, using regex makes the code less verbose than if we were to take each character of the string, check if it is a number and if not throw an exception.

But I was under the assumption that regex also makes the process more efficient. IS this true? I cannot find any evidence on this point. How is regex doing the match behind the scenes? IS it not also iterating over the string and checking each character one by one?

  • 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-09T10:43:13+00:00Added an answer on June 9, 2026 at 10:43 am

    Just for fun, I have run this micro benchmark. The results of the last run (i.e. post JVM warm up / JIT) are below (results are fairly consistent from one run to another anyway):

    regex with numbers 123
    chars with numbers 33
    parseInt with numbers 33
    regex with words 123
    chars with words 34
    parseInt with words 733
    

    In other words, chars is very efficient, Integer.parseInt is as efficient as char IF the string is a number, but awfully slow if the string is not a number. Regex is in between.

    Conclusion

    If you parse a string into a number and you expect the string to be a number in general, using Integer.parseInt is the best solution (efficient and readable). The penalty you get when the string is not a number should be low if it is not too frequent.

    ps: my regex is maybe not optimal, feel free to comment.

    public class TestNumber {
    
        private final static List<String> numbers = new ArrayList<>();
        private final static List<String> words = new ArrayList<>();
    
        public static void main(String args[]) {
            long start, end;
            Random random = new Random();
    
            for (int i = 0; i < 1000000; i++) {
                numbers.add(String.valueOf(i));
                words.add(String.valueOf(i) + "x");
            }
    
            for (int i = 0; i < 5; i++) {
                start = System.nanoTime();
                regex(numbers);
                System.out.println("regex with numbers " + (System.nanoTime() - start) / 1000000);
                start = System.nanoTime();
                chars(numbers);
                System.out.println("chars with numbers " + (System.nanoTime() - start) / 1000000);
                start = System.nanoTime();
                exception(numbers);
                System.out.println("exceptions with numbers " + (System.nanoTime() - start) / 1000000);
    
                start = System.nanoTime();
                regex(words);
                System.out.println("regex with words " + (System.nanoTime() - start) / 1000000);
                start = System.nanoTime();
                chars(words);
                System.out.println("chars with words " + (System.nanoTime() - start) / 1000000);
                start = System.nanoTime();
                exception(words);
                System.out.println("exceptions with words " + (System.nanoTime() - start) / 1000000);
            }
        }
    
        private static int regex(List<String> list) {
            int sum = 0;
            Pattern p = Pattern.compile("[0-9]+");
            for (String s : list) {
                sum += (p.matcher(s).matches() ? 1 : 0);
            }
            return sum;
        }
    
        private static int chars(List<String> list) {
            int sum = 0;
    
            for (String s : list) {
                boolean isNumber = true;
                for (char c : s.toCharArray()) {
                    if (c < '0' || c > '9') {
                        isNumber = false;
                        break;
                    }
                }
                if (isNumber) {
                    sum++;
                }
            }
            return sum;
        }
    
        private static int exception(List<String> list) {
            int sum = 0;
    
            for (String s : list) {
                try {
                    Integer.parseInt(s);
                    sum++;
                } catch (NumberFormatException e) {
                }
            }
            return sum;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following regular expression in java to try and find if
I am using Java Servlet. In there, i have to use regular expression to
I've been struggling with doing some relatively straightforward regular expression matching in Java 1.4.2.
I've been told that there is some overhead in using the Java try-catch mechanism.
I am using the compareTo method in Java to try and check if a
In Java you can try to convert any String value to Integer, but when
Whenever I import java.awt.* and try to use a List I get an error.
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
I try use a integer array in java with the code below: public static
I try to match a regex with java and can't find the right regex

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.