0

I'm using the BeanIO framework to parse files. In my XML code, i want a 3-digit field to only contain a specific range of numbers, from 000 to 199.

I've tried the following (the field must be manipulated as a String) :

<field name="recordType" length="3" minOccurs = "0" regex = "[0-1][0-9][0-9]"/>

It doesn't seem to be working. Any ideas?

Thanks in advance!

ldtcoop
  • 680
  • 4
  • 14

1 Answers1

1

My guess is that maybe you might want to design an expression to cover some other single and double digits, beside three digits, such as:

^([0-1][0-9]{2}|[0-9]{2}|[0-9])$

or,

^([0-1][0-9]{2}|[0-9]{1,2})$

or,

^(?:[0-1][0-9]{2}|[0-9]{1,2})$

Demo

Test

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


public class re{

    public static void main(String[] args){

        final String regex = "^(?:[0-1][0-9]{2}|[0-9]{2}|[0-9])$";
        final String string = "0\n"
             + "1\n"
             + "2\n"
             + "00\n"
             + "11\n"
             + "22\n"
             + "99\n"
             + "100\n"
             + "000\n"
             + "199\n"
             + "200";

        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);

        while (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
            for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("Group " + i + ": " + matcher.group(i));
            }
        }

    }
}

Output

Full match: 0
Full match: 1
Full match: 2
Full match: 00
Full match: 11
Full match: 22
Full match: 99
Full match: 100
Full match: 000
Full match: 199

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Emma
  • 27,428
  • 11
  • 44
  • 69