开发者

Creating a number range from a regex-like descriptor

I am trying to create a number range given a regex. This is the opposite of most questions in trying to validate if a number is in a range. I am going to be given the regex and need to create the range.

For example, given the regex "80055510[34]X" I would like to create the numbers 8005551030 through 8005551049. The "X" can be whatever, it's just what the end users are used to using (in Cisco Call Manager applications).

Given "80055512[12][12]" it would create:

8005551211 8005551212 8005551221 8005551212

Given: "800555120[0-2,9]" 开发者_如何学JAVAit would generate:

8005551200 8005551201 8005551202 8005551209

Is this possible using regular expressions or do I just need to parse things on my own and do it?

I would be coding this in Java if it makes a difference. Thanks.


You may of course use regular expressions to parse the input strings*, using say "\\[(.*?)\\]" to find occurrences of, for instance "[0-2,9]".

Other than that, I doubt that the regexp classes (Pattern, Matcher) of java will be of any help for you here. Unless you do some crazy brute-force and let the java-regexp "filter out" what numbers are valid.


* Provided you're not planning on supporting strange expressions like [[4-8]-[3-9]].


Regular expressions should be used for pattern matching, not parsing. I guess that in your case, you would have to come up with your own mechanism to parse the regular expression.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜