regexboost-regex

boost regex - how to match an even number of digits from a range


I would like to match an even number of digits from a range. Here is a regex which match a number of digits from range:

boost::regex expr("[0-9]{2,20}");

How to modify that regex to match an even number of digits from a range ?


Solution

  • Your pattern [0-9]{2,20} repeats a digit 0-9 from 2 - 20 times.

    You could use an anchor to assert the start ^ and the end $ of the string and repeat matching 2 digits between 1-10 times:

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

    Regex demo