Java Regex Int Validate parseIntegers(String integerString)

Here you can find the source of parseIntegers(String integerString)

Description

Given a string of the form "a,b,c-d,e,...", parse out the integers represented including those within the (hyphenated) ranges.

License

Open Source License

Declaration

public static final int[] parseIntegers(String integerString) 

Method Source Code

//package com.java2s;
/*/*from  www  . j a va  2  s .  c  o m*/
Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
This file is part of the Semantic Discovery Toolkit.
    
The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
The Semantic Discovery Toolkit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /** [-]a - [-]b */
    private static final Pattern RANGE_PATTERN = Pattern.compile("^\\s*(-?\\d+)\\s*-\\s*(-?\\d+)\\s*$");

    /**
     * Given a string of the form "a,b,c-d,e,...", parse out the integers
     * represented including those within the (hyphenated) ranges.
     */
    public static final int[] parseIntegers(String integerString) {
        final List<Integer> integers = new ArrayList<Integer>();
        final String[] pieces = integerString.split(",");

        for (String piece : pieces) {
            collectIntegers(piece, integers);
        }

        final int[] result = new int[integers.size()];
        int index = 0;
        for (Integer integer : integers) {
            result[index++] = integer;
        }
        return result;
    }

    private static final void collectIntegers(String piece, List<Integer> collector) {
        final Matcher m = RANGE_PATTERN.matcher(piece);
        if (m.matches()) {
            final String startString = m.group(1);
            final String endString = m.group(2);
            final int startInt = Integer.parseInt(startString);
            final int endInt = Integer.parseInt(endString);

            if (startInt <= endInt) {
                // counting up
                for (int i = startInt; i <= endInt; ++i) {
                    collector.add(i);
                }
            } else {
                // counting down
                for (int i = startInt; i >= endInt; --i) {
                    collector.add(i);
                }
            }
        } else {
            final Integer integer = new Integer(piece.trim());
            collector.add(integer);
        }
    }
}

Related

  1. parseInteger(String attrVal)
  2. parseInteger(String inStr, Integer def)
  3. parseInteger(String key, String sourceString)
  4. parseInteger(String str)
  5. parseIntegerList(String var0, int var1, int var2)
  6. parseIntToString(String value)