Guest User

Untitled

a guest
Jun 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. Regex NumberExtractor = new Regex("[0-9]{7,16}",RegexOptions.Compiled);
  2.  
  3. /// <summary>
  4. /// Extracts numbers between seven and sixteen digits long from the target file.
  5. /// Example number to be extracted: +923466666666
  6. /// </summary>
  7. /// <param name="TargetFilePath"></param>
  8. /// <returns>List of the matching numbers</returns>
  9. private IEnumerable<ulong> ExtractLongNumbersFromFile(string TargetFilePath)
  10. {
  11.  
  12. if (String.IsNullOrEmpty(TargetFilePath))
  13. throw new ArgumentException("TargetFilePath is null or empty.", "TargetFilePath");
  14.  
  15. if (File.Exists(TargetFilePath) == false)
  16. throw new Exception("Target file does not exist!");
  17.  
  18. FileStream TargetFileStream = null;
  19. StreamReader TargetFileStreamReader = null;
  20. string FileContents = "";
  21. List<ulong> ReturnList = new List<ulong>();
  22.  
  23. try
  24. {
  25. TargetFileStream = new FileStream(TargetFilePath, FileMode.Open);
  26. TargetFileStreamReader = new StreamReader(TargetFileStream);
  27. FileContents = TargetFileStreamReader.ReadToEnd();
  28.  
  29. MatchCollection Matches = NumberExtractor.Matches(FileContents);
  30.  
  31. foreach (Match CurrentMatch in Matches) {
  32. ReturnList.Add(System.Convert.ToUInt64(CurrentMatch.Value));
  33. }
  34.  
  35. }
  36. catch (Exception ex)
  37. {
  38. //Your logging, etc...
  39. }
  40. finally
  41. {
  42. if (TargetFileStream != null) {
  43. TargetFileStream.Close();
  44. TargetFileStream.Dispose();
  45. }
  46.  
  47. if (TargetFileStreamReader != null)
  48. {
  49. TargetFileStreamReader.Dispose();
  50. }
  51. }
  52.  
  53. return (IEnumerable<ulong>)ReturnList;
  54.  
  55.  
  56. }
  57.  
  58. List<ulong> Numbers = (List<ulong>)ExtractLongNumbersFromFile(@"v:TestExtract.txt");
  59.  
  60. using System;
  61. using System.Collections.Generic;
  62. using System.Linq;
  63. using System.Text;
  64. using NUnit.Framework;
  65. using System.Text.RegularExpressions;
  66.  
  67. namespace SO.NumberExtractor.Test
  68. {
  69. public class NumberExtracter
  70. {
  71. public List<string> ExtractNumbers(string lines)
  72. {
  73. List<string> numbers = new List<string>();
  74. string[] seperator = { System.Environment.NewLine };
  75. string[] seperatedLines = lines.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
  76.  
  77. foreach (string line in seperatedLines)
  78. {
  79. string s = ExtractNumber(line);
  80. numbers.Add(s);
  81. }
  82.  
  83. return numbers;
  84. }
  85.  
  86. public string ExtractNumber(string line)
  87. {
  88. string s = line.Split(',').Last<string>().Trim('"');
  89. return s;
  90. }
  91.  
  92. public string ExtractNumberWithoutLinq(string line)
  93. {
  94. string[] fields = line.Split(',');
  95. string s = fields[fields.Length - 1];
  96. s = s.Trim('"');
  97.  
  98. return s;
  99. }
  100. }
  101.  
  102. [TestFixture]
  103. public class NumberExtracterTest
  104. {
  105. private readonly string LINE1 = "AT+CMGL="ALL" +CMGL: 5566,"REC READ","Ufone" Dear customer, your DAY_BUCKET subscription will expire on 02/05/09 +CMGL: 5565,"REC READ","+923466666666"";
  106. private readonly string LINE2 = "AT+CMGL="ALL" +CMGL: 5566,"REC READ","Ufone" Dear customer, your DAY_BUCKET subscription will expire on 02/05/09 +CMGL: 5565,"REC READ","+923466666667"";
  107. private readonly string LINE3 = "AT+CMGL="ALL" +CMGL: 5566,"REC READ","Ufone" Dear customer, your DAY_BUCKET subscription will expire on 02/05/09 +CMGL: 5565,"REC READ","+923466666668"";
  108.  
  109. [Test]
  110. public void ExtractOneLineWithoutLinq()
  111. {
  112. string expected = "+923466666666";
  113.  
  114. NumberExtracter c = new NumberExtracter();
  115. string result = c.ExtractNumberWithoutLinq(LINE1);
  116.  
  117. Assert.AreEqual(expected, result);
  118. }
  119.  
  120. [Test]
  121. public void ExtractOneLineUsingLinq()
  122. {
  123. string expected = "+923466666666";
  124.  
  125. NumberExtracter c = new NumberExtracter();
  126. string result = c.ExtractNumber(LINE1);
  127.  
  128. Assert.AreEqual(expected, result);
  129. }
  130.  
  131. [Test]
  132. public void ExtractMultipleLines()
  133. {
  134. StringBuilder sb = new StringBuilder();
  135. sb.AppendLine(LINE1);
  136. sb.AppendLine(LINE2);
  137. sb.AppendLine(LINE3);
  138.  
  139. NumberExtracter ne = new NumberExtracter();
  140. List<string> extractedNumbers = ne.ExtractNumbers(sb.ToString());
  141.  
  142. string expectedFirst = "+923466666666";
  143. string expectedSecond = "+923466666667";
  144. string expectedThird = "+923466666668";
  145.  
  146. Assert.AreEqual(expectedFirst, extractedNumbers[0]);
  147. Assert.AreEqual(expectedSecond, extractedNumbers[1]);
  148. Assert.AreEqual(expectedThird, extractedNumbers[2]);
  149. }
  150. }
  151. }
  152.  
  153. foreach ( string line in File.ReadAllLines(@"c:pathtofile.txt") ) {
  154. Match result = Regex.Match(line, @"+(d+)""$");
  155. if ( result.Success ) {
  156. var number = result.Groups[1].Value;
  157. // do what you want with the number
  158. }
  159. }
Add Comment
Please, Sign In to add comment