| 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. | /* Sifter v1.0
*
* Author: mc0
*
* About: This program is used to retrive strings of information from text files using regex and
* regex groups.
*
* Usage: fileparse -l input.txt -o output.txt -v -e regex-expression -d output-expression (USE THE SAME GROUPS)
*
*/
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;
using System.Web;
namespace fileParse
{
class Program
{
private ArrayList sGroups;
private string fList, fOut, sExp, sDef;
private bool verbose;
static void Main(string[] args)
{
Program p = new Program(args);
}
public Program(string[] args)
{
if (readArgs(args) == false)
{
usage();
return;
}
ThreadStart tstart = new ThreadStart(sifter);
Thread thread = new Thread(tstart);
thread.Start();
}
private void sifter()
{
sGroups = findGroups(sExp);
Regex rIn = new Regex(sExp);
MatchCollection mData;
StreamReader reader = new StreamReader(fList);
try
{
do
{
mData = rIn.Matches(reader.ReadLine());
foreach (Match mData1 in mData)
{
writeOutput(mData1);
}
}
while (reader.Peek() >= 0);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
reader.Close();
}
}
private void writeOutput(Match mData)
{
ArrayList sGroups = findGroups(sDef);
string output = sDef;
for(int i = 0;i < sGroups.Count;i++)
{
output = output.Replace("<" + sGroups[i].ToString() + ">", mData.Groups[sGroups[i].ToString()].Value);
}
output = HttpUtility.UrlDecode(output);
StreamWriter writer = File.AppendText(fOut);
try
{
writer.WriteLine(output);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
writer.Close();
}
if (verbose)
{
Console.WriteLine(output);
}
}
private ArrayList findGroups(string sExp)
{
int i = 0;
ArrayList sGroups = new ArrayList();
Regex rGroup = new Regex(@"<[^>]*?>");
MatchCollection m = rGroup.Matches(sExp);
foreach (Match match in m)
{
sGroups.Add(match.ToString().Substring(1, match.ToString().Length - 2));
i++;
}
return sGroups;
}
private bool readArgs(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
try
{
switch (args[i])
{
case "-l":
fList = args[i + 1];
break;
case "-o":
fOut = args[i + 1];
break;
case "-v":
verbose = true;
break;
case "-e":
sExp = args[i + 1];
break;
case "-d":
sDef = args[i + 1];
break;
}
}
catch
{
return false;
}
}
if(String.IsNullOrEmpty(fList) ||
String.IsNullOrEmpty(fOut) ||
String.IsNullOrEmpty(sExp))
{
return false;
}
return true;
}
private void usage()
{
Console.WriteLine("Sifter v1.0");
Console.WriteLine("Usage: \n");
Console.WriteLine(" -l <FileName> REQUIRED");
Console.WriteLine(" Specify the name of the file containing the");
Console.WriteLine(" information you wish to have parsed here.\n");
Console.WriteLine(" -o <FileName> REQUIRED");
Console.WriteLine(" Specify the name of the file you wish to have ");
Console.WriteLine(" your parsed information written to here.\n");
Console.WriteLine(" -v");
Console.WriteLine(" Verbose option.\n");
Console.WriteLine(" -e <String> REQUIRED");
Console.WriteLine(" Specify the expression you wish to search for ");
Console.WriteLine(" here (Learn how to use regex & regex groups!).\n");
Console.WriteLine(" -d <String> REQUIRED");
Console.WriteLine(" Specify the output string expression ");
Console.WriteLine(" here (Learn how to use regex & regex groups!).\n");
}
}
} |