Rosetta Code:Village Pump/Suggest a programming task: Difference between revisions
Content added Content deleted
(→= Partitioning: fixed section header name formatting.) |
|||
Line 712: | Line 712: | ||
=== Generate a Regular Expression for a Range of Integers === |
=== Generate a Regular Expression for a Range of Integers === |
||
Write a function that, given the integers 0 <= min < = max, returns a regular expression that will match the decimal representation of any integer in the range [min, max]. You may assume there are no thousand-separators, leading 0's, or other problematic characters in the string to be processed. You may also assume the caller of your function will prepend/append appropriate anchors (e.g., ^, $, or \b) depending on need, so don't include them. Don't use \d as an alias for [0-9] since \d can match weird unicode characters on some systems. |
Write a function that, given the integers 0 <= ''min'' < = ''max'', returns a regular expression that will match the decimal representation of any integer in the range [''min'', ''max'']. You may assume there are no thousand-separators, leading 0's, or other problematic characters in the string to be processed. You may also assume the caller of your function will prepend/append appropriate anchors (e.g., ^, $, or \b) depending on need, so don't include them. Don't use \d as an alias for [0-9] since \d can match weird unicode characters on some systems. |
||
Example: |
Example: |
||
min = 1 |
min = 1 |
||
max = 31 |
max = 31 |
||
possible result = [1-9]|[1-2][0-9]|3[0-1] |
possible result = [1-9]|[1-2][0-9]|3[0-1] |
||
possible result = [1-9]|[12][0-9]|3[01] |
possible result = [1-9]|[12][0-9]|3[01] |
||
Example: |
Example: |
||
min = 91 |
min = 91 |
||
max = 417 |
max = 417 |
||
possible result = 9[1-9]|[1-3][0-9]{2}|4(0[0-9]|1[0-7]) |
possible result = 9[1-9]|[1-3][0-9]{2}|4(0[0-9]|1[0-7]) |
||
possible result = 9[1-9]|[1-3][0-9][0-9]|4(0[0-9]|1[0-7]) |
possible result = 9[1-9]|[1-3][0-9][0-9]|4(0[0-9]|1[0-7]) |
||