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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:06:07+00:00 2026-05-15T01:06:07+00:00

I don’t think it is hard, just tedious to write: Some small free (as

  • 0

I don’t think it is hard, just tedious to write: Some small free (as in beer) library where I can put in a String like 1,2-9,33- and it can tell me whether a given number matches that expression. Just like most programs have in their print range dialogs. Special functions for matching odd or even numbers only, or matching every number that is 2 mod 5 (or something like that) would be nice, but not needed.

The only operation I have to perform on this list is whether the range contains a given (nonnegative) integer value; more operations like max/min value (if they exist) or an iterator would be nice, of course.

What would be needed that it does not occupy lots of RAM if anyone enters 1-10000000 but the only number I will ever query is 12345 🙂

(To implement it, I would parse a list into several (min/max/value/mod) pairs, like 1,10,0,1 for 1-10 or 11,33,1,2 for 1-33odd, or 12,62,2,10 for 12-62/10 (i. e. 12, 22, 32, …, 62) and then check each number for all the intervals. Open intervals by using Integer.MaxValue etc. If there are no libs, any ideas to do it better/more efficient?)

  • 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-15T01:06:08+00:00Added an answer on May 15, 2026 at 1:06 am

    I decided to code it myself indeed. Use at your own risk :-)

    /* 
     * NumberExpression.java - a simple number expression parser
     * 
     * Copyright (c) 2010 Michael Schierl
     * 
     * All rights reserved.
     * 
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     * 
     * - Redistributions of source code must retain the above copyright notice,
     *   this list of conditions and the following disclaimer.
     *   
     * - Redistributions in binary form must reproduce the above copyright
     *   notice, this list of conditions and the following disclaimer in the
     *   documentation and/or other materials provided with the distribution.
     *   
     * - Neither name of the copyright holders nor the names of its
     *   contributors may be used to endorse or promote products derived from
     *   this software without specific prior written permission.
     *   
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS
     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    package numberexpression;
    
    /**
     * An expression that matches nonnegative numbers. This supports cron-like
     * expressions, like <code>1,3-6,100-200,666,1000-3000/5,400-/7</code>,
     * <code>-100,102-</code> or <code>*</code>. Odd or even numbers can be
     * matched either by cron's step syntax, or by suffixing a simple range
     * (without step values) with <code>e</code> or <code>o</code>.
     * 
     * @author Michael Schierl
     */
    public class NumberExpression {
    
        private final NumberRange[] ranges;
        private final int min, max;
    
        /**
         * Create a new {@link NumberExpression}.
         * 
         * @param pattern
         *            the expression pattern.
         * @throws IllegalArgumentException
         *             if the pattern is malformed
         */
        public NumberExpression(String pattern) {
            String[] parts = pattern.toLowerCase().split(",",-1);
            ranges = new NumberRange[parts.length];
            int min = Integer.MAX_VALUE, max = 0;
            for (int i = 0; i < ranges.length; i++) {
                String part = parts[i];
                try {
                    if (part.equals("*")) {
                        ranges[i] = new NumberRange(0, Integer.MAX_VALUE, 0, 1);
                    } else if (part.matches("\\*/\\d+")) {
                        ranges[i] = new NumberRange(0, Integer.MAX_VALUE, 0, Integer.parseInt(part.substring(2)));
                    } else if (part.matches("\\d+")) {
                        int value = Integer.parseInt(part);
                        ranges[i] = new NumberRange(value, value, 0, 1);
                    } else if (part.matches("\\d*-\\d*")) {
                        String[] limits = part.split("-", -1);
                        int from = limits[0].length() == 0 ? 0 : Integer.parseInt(limits[0]);
                        int to = limits[1].length() == 0 ? Integer.MAX_VALUE : Integer.parseInt(limits[1]);
                        if (to < from)
                            throw new IllegalArgumentException("Invalid pattern: " + part);
                        ranges[i] = new NumberRange(from, to, 0, 1);
                    } else if (part.matches("\\d*-\\d*/\\d+")) {
                        String[] rangeAndModulus = part.split("/", -1);
                        String[] limits = rangeAndModulus[0].split("-", -1);
                        int from = limits[0].length() == 0 ? 0 : Integer.parseInt(limits[0]);
                        int to = limits[1].length() == 0 ? Integer.MAX_VALUE : Integer.parseInt(limits[1]);
                        int modulus = Integer.parseInt(rangeAndModulus[1]);
                        if (to < from)
                            throw new IllegalArgumentException("Invalid pattern: " + part);
                        ranges[i] = new NumberRange(from, to, from % modulus, modulus);
                    } else if (part.matches("\\d*-\\d*[eo]")) {
                        String[] limits = part.substring(0, part.length() - 1).split("-", -1);
                        int from = limits[0].length() == 0 ? 0 : Integer.parseInt(limits[0]);
                        int to = limits[1].length() == 0 ? Integer.MAX_VALUE : Integer.parseInt(limits[1]);
                        if (to < from)
                            throw new IllegalArgumentException("Invalid pattern: " + part);
                        ranges[i] = new NumberRange(from, to, part.charAt(part.length() - 1) == 'o' ? 1 : 0, 2);
                    } else {
                        throw new IllegalArgumentException("Invalid pattern: " + part);
                    }
                    max = Math.max(max, ranges[i].getMax());
                    min = Math.min(min, ranges[i].getMin());
                } catch (NumberFormatException ex) {
                    throw new IllegalArgumentException("Invalid pattern: " + part);
                }
            }
            this.max = max;
            this.min = min;
        }
    
        /**
         * Check whether this number expression matches the given number.
         * 
         * @param number
         *            the number to check against
         * @return whether the expression matches the number
         */
        public boolean matches(int number) {
            if (number < min || number > max)
                return false;
            for (int i = 0; i < ranges.length; i++) {
                if (ranges[i].matches(number))
                    return true;
            }
            return false;
        }
    
        /**
         * Return the minimum number that can be matched.
         */
        public int getMinimum() { return min; }
    
        /**
         * Return the maximum number that can be matched.
         */
        public int getMaximum() { return max; }
    
        private static class NumberRange {
            private final int min, max, remainder, modulus;
    
            NumberRange(int min, int max, int remainder, int modulus) {
                this.min = min;
                this.max = max;
                this.remainder = remainder;
                this.modulus = modulus;
            }
    
            boolean matches(int number) {
                return number >= min && number <= max && number % modulus == remainder;
            }
    
            int getMin() { return min; }  
            int getMax() { return max; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don’t think I’ve grokked currying yet. I understand what it does, and how
I don't know if anyone has seen this issue before but I'm just stumped.
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I don't know when to add to a dataset a tableadapter or a query
I don't edit CSS very often, and almost every time I need to go
I don't understand where the extra bits are coming from in this article about
I don't want PHP errors to display /html, but I want them to display
I don't remember whether I was dreaming or not but I seem to recall
I don't expect a straightforward silver bullet answer to this, but what are 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.