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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:11:20+00:00 2026-05-23T05:11:20+00:00

I need to generate, at run time, a regular expression that will match a

  • 0

I need to generate, at run time, a regular expression that will match a range of numeric values.

For example: At run time I may discover that I need a regular expression matching all the files in the “range” a-261-b.something to a-543-b.something.

I need to generate a regular expression that will match all of this files. Any ideas?

I need it in Java, so if anyone know any Java-specific way to so this, it’s also acceptable.

  • 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-23T05:11:21+00:00Added an answer on May 23, 2026 at 5:11 am

    Whether or not regular expressions are well suited for this task is debatable. Most people would probably argue that it’s not.

    As I understand it however, you have no choice as the API you’re using takes a regular expression as argument, so here goes…

    Code

    public class NumericRangeRegexGenerator {
    
        private static String baseRange(String num, boolean up, boolean leading1) {
    
            char c = num.charAt(0);
            char low  = up ? c : leading1 ? '1' : '0';
            char high = up ? '9' : c;
    
            if (num.length() == 1)
                return charClass(low, high);
    
            String re = c + "(" + baseRange(num.substring(1), up, false) + ")";
    
            if (up) low++; else high--;
    
            if (low <= high)
                re += "|" + charClass(low, high) + nDigits(num.length() - 1);
    
            return re;
        }
    
        private static String charClass(char b, char e) {
            return String.format(b==e ? "%c" : e-b>1 ? "[%c-%c]" : "[%c%c]", b, e);
        }
    
        private static String nDigits(int n) {
            return nDigits(n, n);
        }
    
        private static String nDigits(int n, int m) {
            return "[0-9]" + String.format(n==m ? n==1 ? "":"{%d}":"{%d,%d}", n, m);
        }
    
        private static String eqLengths(String from, String to) {
    
            char fc = from.charAt(0), tc = to.charAt(0);
    
            if (from.length() == 1 && to.length() == 1)
                return charClass(fc, tc);
    
            if (fc == tc)
                return fc + "("+rangeRegex(from.substring(1), to.substring(1))+")";
    
            String re = fc + "(" + baseRange(from.substring(1), true, false) + ")|"
                      + tc + "(" + baseRange(to.substring(1),  false, false) + ")";
    
            if (++fc <= --tc)
                re += "|" + charClass(fc, tc) + nDigits(from.length() - 1);
    
            return re;
        }    
    
        private static String nonEqLengths(String from, String to) {
            String re = baseRange(from,true,false) + "|" + baseRange(to,false,true);
            if (to.length() - from.length() > 1)
                re += "|[1-9]" + nDigits(from.length(), to.length() - 2);
            return re;
        }
    
        public static String rangeRegex(int n, int m) {
            return rangeRegex("" + n, "" + m);
        }
    
        public static String rangeRegex(String n, String m) {
            return n.length() == m.length() ? eqLengths(n, m) : nonEqLengths(n, m);
        }
    
    }
    

    Usage

    // Generate expression for range 123 - 321
    String regexp = NumericRangeRegexGenerator.rangeRegex(123, 321);
    

    Explanation

    A brief explanation of the code follows.

    Ranges on the shape 0000–abcd and abcd–9999

    First we note that matching ranges such as 0000–abcd is fairly easy.

    An expression covering for instance 000–527 can be expressed as

    • [0-4] followed by two arbitrary digits, or
    • 5 followed by 00–27 (which is resolved recursively!)

    Ranges on the shape 1000–abcd and abcd–9999 are just as easy.

    Lower limit, upper limit of different lengths.

    If the “from”-number is shorter than the “to”-number it is fairly straight forward.

    Assume for instance that the from-number has 3 digits and the to-number has 7 digits. The expression can then be composed as follows:

    • from–999 (as described above),
    • Any 4, 5 or 6 digit number: [1-9][0-9]{3-5}, or
    • 1000000–to (as described above)

    Lower limit / upper limit of equal lengths.

    This is the trickiest situation (still not that tricky though!)

    The solution is, again, best described by an example. Consider the range 273 – 548. The expression can be composed by the following parts:

    • 2 followed by 73–99 (latter part described above),
    • [34] followed by any two digits, or
    • 5 followed by 00–48 (latter part described above)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate a new interface at run-time with all the same members
For one of my projects, I need to generate at run time some classes,
I need to be able to generate links to sites that all run off
I need to generate some passwords, I want to avoid characters that can be
I need to generate thumbnails from a set of jpg's that need to have
I need to generate multiple random values under SQL Server 2005 and somehow this
We need to generate LINQ queries which are 100% unknown during coding (design time).
I need to create a strongly typed dataset during run-time for the user preferred
I've got a thread that sends emails around. I need to generate ActionLinks as
I need generate thumbnails for a bunch of jpegs (200,000+) but I want 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.