यहाँ कुछ मैं कुछ इसी तरह के लिए पकाया जाता है।
1 single number
1-5 range
-5 range from (firstpage) up to 5
5- range from 5 up to (lastpage)
.. can use .. instead of -
;, can use both semicolon, comma, and space, as separators
यह डुप्लिकेट मानों की जांच नहीं करता है, तो सेट 1,5, -10 अनुक्रम 1, 5, 1, 2 का उत्पादन करेगा:
यह पर्वतमाला के निम्नलिखित प्रकार संभालती है , 3, 4, 5, 6, 7, 8, 9, 10।
public class RangeParser
{
public static IEnumerable<Int32> Parse(String s, Int32 firstPage, Int32 lastPage)
{
String[] parts = s.Split(' ', ';', ',');
Regex reRange = new Regex(@"^\s*((?<from>\d+)|(?<from>\d+)(?<sep>(-|\.\.))(?<to>\d+)|(?<sep>(-|\.\.))(?<to>\d+)|(?<from>\d+)(?<sep>(-|\.\.)))\s*$");
foreach (String part in parts)
{
Match maRange = reRange.Match(part);
if (maRange.Success)
{
Group gFrom = maRange.Groups["from"];
Group gTo = maRange.Groups["to"];
Group gSep = maRange.Groups["sep"];
if (gSep.Success)
{
Int32 from = firstPage;
Int32 to = lastPage;
if (gFrom.Success)
from = Int32.Parse(gFrom.Value);
if (gTo.Success)
to = Int32.Parse(gTo.Value);
for (Int32 page = from; page <= to; page++)
yield return page;
}
else
yield return Int32.Parse(gFrom.Value);
}
}
}
}
स्रोत
2008-09-02 19:37:43
रिवर्स कार्रवाई करने के लिए, http://stackoverflow.com/questions/7688881/convert-list-to-number-range-string देखना – Grhm