[英]Trying to Validate Emails using C# with a Script in SSIS, but would like to validate only the email column
我正在使用SSIS腳本組件來驗證CSV文件內容。 帶有信息的CSV文件標題的內容是:FName,LName,Email,地址,城市,州,郵政編碼。 我希望正則表達式僅驗證“電子郵件”列。 有辦法嗎?
[Code]
public static bool IsValid(string EmailAddress)
{
string strPattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]
{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
bool bFlag = false;
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(strPattern);
System.Text.RegularExpressions.Match match = regex.Match(EmailAddress);
if (match.Success) bFlag = true;
return bFlag;
}
static void TestSeparatingValidTextData()
{
try
{
//bool isFirst = true;
//using (StreamReader sr = new StreamReader(filePath))
//{
// while (sr.Peek() != -1)
// {
// if (isFirst)
// {
// isFirst = false;
// continue;
// }
// }
//}
System.IO.StreamReader objReadFile = new
System.IO.StreamReader(@"C:\_BISolutions\ClinicDailyData\Bellevue\
Bellevue_EmailsToValidate\Bellevue_EmailToValidateData.csv");
System.IO.StreamWriter objWriteValidFile = new
System.IO.StreamWriter(@"C:\_BISolutions\
ClinicDailyData\Bellevue\Bellevue_ValidEmail\Bellevue_ValidEmailData.csv"
, false);
System.IO.StreamWriter objWriteInValidFile = new System.IO.StreamWriter(@"C:\_BISolutions\ClinicDailyData\Bellevue\
Bellevue_InValidEmail\Bellevue_InValidEmailData.csv", false);
string strLineData = "";
while ((strLineData = objReadFile.ReadLine()) != null)
//&& objReadFile.Peek() != -1)
{ /*************************/
//This first if statement bypasses the first line.
//if (isFirst)
//{
// isFirst = false;
// continue;
//}
/*************************/
if (IsValid(strLineData))
{
objWriteValidFile.WriteLine(strLineData);
}
else
{
objWriteInValidFile.WriteLine(strLineData);
}
}
objReadFile.Close();
objWriteValidFile.Close();
objWriteInValidFile.Close();
}
catch (System.Exception objException)
{
System.Console.WriteLine(objException.ToString());
throw objException; //Must add this for the Main method to catch
this exception.
}
}
/// <summary>
/// This method is called when this script task executes in the control
flow.
/// Before returning from this method, set the value of Dts.TaskResult to
indicate success or failure.
/// To open Help, press F1.
/// </summary>
public void Main()
{
try
{
TestSeparatingValidTextData();
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception objException)
{
string strMessage = @"On Error, please check the file is in
C:\_BISolutions\ClinicDailyData\Bellevue\
Folder";
Dts.Events.FireError(0, strMessage, objException.ToString()
, string.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this
class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
[/Code]
我只想將具有標題為email的標頭的csv文件中的一列作為目標,以便RegEx進行檢查(不是所有列)。 任何幫助將不勝感激。
您當前正在將整個行傳遞給IsValid
函數。 您需要對其進行拆分,然后僅傳遞所需的條目。 例如,
var firstLine = objReadFile.ReadLine(); // pop off the header line
objWriteValidFile.WriteLine(firstLine);
objWriteInValidFile.WriteLine(firstLine);
while ((strLineData = objReadFile.ReadLine()) != null)
//&& objReadFile.Peek() != -1)
{ /*************************/
//This first if statement bypasses the first line.
//if (isFirst)
//{
// isFirst = false;
// continue;
//}
/*************************/
var parts = strLineData.Split(','); // assuming that ',' is your delimiter character
if (parts.Length >= 2 && IsValid(parts[2]))
{
// write to valid file
}
else
{
// write to invalid file
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.