Skip to content

Instantly share code, notes, and snippets.

@Unripe01
Created November 29, 2012 08:52
Show Gist options
  • Save Unripe01/4167676 to your computer and use it in GitHub Desktop.
Save Unripe01/4167676 to your computer and use it in GitHub Desktop.
I want to write more simple [if] statement at [//Add a string all matching] comments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace SearchStringWithRegularExpressions
{
class Program
{
/// <summary>
/// **Setting** Change The Encod Text from Input File.
/// </summary>
private const string _FileEncoding = "Shift_JIS";
/// <summary>
/// **Setting** specify the matching pattern of RegularExpressions
/// </summary>
private const string _MatchingPattern = "\"Core_E[0-9]{5,}";
/// <summary>
/// **Setting** when set to false , Duplicate results are output to ignore
/// </summary>
private const bool _AllowDuplicateChars = false;
/// <summary>
/// Entry Point
/// </summary>
/// <param name="args"></param>
static void Main( string[] args )
{
// argument check
string inputFilePath;
if( ! IsArgumentValid( args, out inputFilePath ) )
{
return;
}
// input file
//StringBuilder sb = new StringBuilder();
List<string> sL = new List<string>();
using( StreamReader sr = new StreamReader(
inputFilePath, Encoding.GetEncoding( _FileEncoding ) ) )
{
string line;
while( ( line = sr.ReadLine() ) != null )
{
// saving string by RegularExpressions
Regex r = new System.Text.RegularExpressions.Regex(
_MatchingPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase );
System.Text.RegularExpressions.Match m = r.Match( line );
while( m.Success )
{
//Add a string all matching
if( ! _AllowDuplicateChars )
{
if( !sL.Contains( m.Value ) )
{
sL.Add( m.Value );
}
}
else
{
//[sL.Add( m.Value );] also includes two ... Unattractive...
sL.Add( m.Value );
}
m = m.NextMatch();
}
}
}
// output file
System.Text.Encoding enc = System.Text.Encoding.GetEncoding( _FileEncoding );
string outputFilePath = Path.Combine(
Path.GetDirectoryName( inputFilePath ) //Directory
, Path.GetFileNameWithoutExtension( inputFilePath ) + "_Reviewed" //Filename
+ Path.GetExtension( inputFilePath ) ); // Extension
//always overwrite
string outputLine = String.Join( System.Environment.NewLine, sL.ToArray() );
System.IO.File.WriteAllText( outputFilePath, outputLine, enc );
Console.WriteLine( "Complete File Review: \nOutput File Name Is " + outputFilePath );
}
/// <summary>
/// check the validity of the argument.
/// </summary>
/// <param name="args"></param>
/// <returns>args Length == 0 : false , File is Not exist : false </returns>
private static bool IsArgumentValid( string[] args, out string filePath )
{
filePath = "";
// argument check
if( args.Length == 0 )
{
Console.WriteLine( "Error No Argument" );
return false;
}
// File Exists
foreach( string s in args )
{
if( ! File.Exists( s ) )
{
Console.WriteLine( "Error No Argument" );
return false;
}
filePath = s;
break;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment