Anonymous user
Rosetta Code:Village Pump/Suggest a programming task: Difference between revisions
Rosetta Code:Village Pump/Suggest a programming task (view source)
Revision as of 00:58, 26 February 2017
, 7 years ago→Generate a Regular Expression for a Range of Integers
Line 714:
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''':
* min = 1
* max = 31
* possible result = [1-9]|[1-2][0-9]|3[0-1]
* possible result = [1-9]|[12][0-9]|3[01]
'''Example''':
* min = 91
* 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][0-9]|4(0[0-9]|1[0-7])
Hint: Use a recursive, divide-and-conquer approach. You will have to handle cases where max contains more digits than min.
|