JAVA regularity

question: enter parameters of two arbitrary integers, such as 88-3333, and return a regular expression that matches all values in the range.
A problem that looks simple at first, but then finds that the implementation is too complex (not sure whether it is correct), what is the minimalist way?

Feb.28,2021

regular is not good at doing this, and it's not that it can't be done

.

generate a regular expression based on to match any range of values , and generate such a regex for your example in Regex_For_Range :

\b0*(1[1-9][0-9]|[2-9][0-9]{2}|1[0-9]{3}|2[01][0-9]{2}|22[0-2][0-9]|223[0-4])\b


can fulfill your needs:

The

process is as follows (still follow the Regex generator steps):

first of all, it is divided into equal length ranges:

110-999
1000-2234

second, the range in which the breaker produces a simple regexes:

110-199
200-999
1000-1999
2000-2199
2200-2229
2230-2234

change each range to regex:

1 [1-9] [0-9]
[2-9][0-9]{2}
1 [0-9] {3}
2 [01] [0-9] {2}
22 [0-2] [0-9]
223 [0-4]

collapse adjacent decimal digits: 1 [1-9] [0-9] [2-9] [0-9] {2} 1 [0-9] {3} 2 [01] [0-9] {2} 22 [0-2] [0-9] 223 [0-4]

combine the above regex:

0*(1[1-9][0-9]|[2-9][0-9]{2}|1[0-9]{3}|2[01][0-9]{2}|22[0-2][0-9]|223[0-4])

next, we will try to use trees to decompose common prefixes:
analyze trees based on regex prefixes:

.1 [1-9] [0-9]
+ [0-9] {3}
+ [2-9] [0-9] {2}
+ 2 [01] [0-9] {2}
+ 2 [0-2] [0-9]
+ 3 [0-4]

convert the analysis tree to regular:

0*(1([1-9][0-9]|[0-9]{3})|[2-9][0-9]{2}|2([01][0-9]{2}|2([0-2][0-9]|3[0-4])))

We choose the shorter one as our result.

\b0*(1[1-9][0-9]|[2-9][0-9]{2}|1[0-9]{3}|2[01][0-9]{2}|22[0-2][0-9]|223[0-4])\b



see

How to match numbers between X and Y with regexp?

Menu