KingKoelsch
KingKoelsch

Reputation: 129

Regex: Numeric Range java

I have a little generator which allows me to create a string out of a regex-expression. For something like a German license plate it is pretty easy to do. ([A-Z]{1,3}[- ][A-Z]{1,2}[0-9]{1,4}) -> e.g. "CD-B802"

public String generate() {
    String forReturn = null;
    for (String rule : Generator.read(fileRegexConfig)) {
        try {
            Xeger generator = new Xeger(rule);
            forReturn  = generator.generate();
        } catch (Exception e) {
            System.err.println(rule + ':' + e.getMessage());
        }
    }
    return forReturn;
}

public static String[] read(String str) {
    List<String> list = new ArrayList<String>();
    try {
        BufferedReader in = new BufferedReader(new FileReader(str));
        String zeile = null;
        while ((zeile = in.readLine()) != null) {
            if (zeile != null && zeile.trim().length() > 0)
                list.add(zeile);
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (String[]) list.toArray(new String[0]);
}

The Problem I have is, how can I build a regex for a range of int. For example I try to find a way to describe an area of validity like [37-78].

according to http://www.regular-expressions.info/numericranges.html it is easy to describe [0-x] but I can't find a way to solve my problem.

Upvotes: 1

Views: 216

Answers (1)

sp00m
sp00m

Reputation: 48807

RegexForRange could help you:

To match the range [37;78]: (3[7-9]|[4-6][0-9]|7[0-8])

Upvotes: 3

Related Questions