markus / KCOM / RegexCollection.cs @ c4a8d205
이력 | 보기 | 이력해설 | 다운로드 (1.48 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Text.RegularExpressions; |
6 |
|
7 |
namespace KCOM |
8 |
{ |
9 |
public class RangeParser |
10 |
{ |
11 |
//프린트 페이지 검색 키워드 변환 |
12 |
public static IEnumerable<Int32> Parse(String s, Int32 firstPage, Int32 lastPage) |
13 |
{ |
14 |
String[] parts = s.Split(' ', ';', ','); |
15 |
Regex reRange = new Regex(@"^\s*((?<from>\d+)|(?<from>\d+)(?<sep>(-|\.\.))(?<to>\d+)|(?<sep>(-|\.\.))(?<to>\d+)|(?<from>\d+)(?<sep>(-|\.\.)))\s*$"); |
16 |
foreach (String part in parts) |
17 |
{ |
18 |
Match maRange = reRange.Match(part); |
19 |
if (maRange.Success) |
20 |
{ |
21 |
Group gFrom = maRange.Groups["from"]; |
22 |
Group gTo = maRange.Groups["to"]; |
23 |
Group gSep = maRange.Groups["sep"]; |
24 |
|
25 |
if (gSep.Success) |
26 |
{ |
27 |
Int32 from = firstPage; |
28 |
Int32 to = lastPage; |
29 |
if (gFrom.Success) |
30 |
from = Int32.Parse(gFrom.Value); |
31 |
if (gTo.Success) |
32 |
to = Int32.Parse(gTo.Value); |
33 |
for (Int32 page = from; page <= to; page++) |
34 |
yield return page; |
35 |
} |
36 |
else |
37 |
yield return Int32.Parse(gFrom.Value); |
38 |
} |
39 |
} |
40 |
} |
41 |
} |
42 |
} |