我有一些代码会读取CSV文件的每一行,如果该值与正确的值不匹配,它会将其添加到错误列表中,并返回给用户屏幕。我遇到的问题是正则表达式本身。
protected void ReadData(string filePath, bool upload)
{
StringBuilder sb = new StringBuilder();
#region upload
if (upload == true) // CSV file upload chosen
{
using (CsvReader csv = new CsvReader(new StreamReader(filePath), true)) // Cache CSV file to memory
{
int fieldCount = csv.FieldCount; // Total number of fields per row
string[] headers = csv.GetFieldHeaders(); // Correct CSV headers stored in array
SortedList<int, string> errorList = new SortedList<int, string>(); // This list will contain error values
bool errorFlag = false;
int errorCount = 0;
// Check if headers are correct first before reading data
if (headers[0] != "first name" || headers[1] != "last name" || headers[2] != "job title" || headers[3] != "email address" || headers[4] != "telephone number" || headers[5] != "company" || headers[6] != "research manager" || headers[7] != "user card number")
{
sb.Append("Headers are incorrect");
}
else
{
while (csv.ReadNextRecord())
try
{
//Check csv obj data for valid values
for (int i = 0; i < fieldCount; i++)
{
if (i == 0 || i == 1) // FirstName and LastName
{
if (Regex.IsMatch(csv[i].ToString(), "[a-zA-Z]", RegexOptions.IgnoreCase)) //REGEX letters only min of 5 char max of 20
{
errorList.Add(errorCount, csv[i]);
errorCount += 1;
errorFlag = true;
string text = csv[i].ToString();
}
}
else if (i == 5) // Company name
{
string text = csv[i];
text.Replace("&", "and");
}
}
if (errorFlag == true)
{
sb.Append("<b>" + "Number of Error: " + errorCount + "</b>");
sb.Append("<ul>");
foreach (KeyValuePair<int, string> key in errorList)
{
sb.Append("<li>" + key.Value + "</li>");
}
}
else // All validation checks equaled to false. Create User
{
ORCLdap.CreateUserAccount(rootLDAPPath, svcUsername, svcPassword, csv[0], csv[1], csv[2], csv[3], csv[4], csv[5], csv[7]);
sb.Append("<b>New user data uploaded successfully</b>");
}
}// end of try
catch (Exception ex)
{
sb.Append(ex.ToString());
}
finally
{
lblMessage.Text = sb.ToString();
sb.Remove(0, sb.Length);
}
}
}
#endregion
lblMessage.text包含此html:
错误数:4
- David1212
- 史密斯
- Nick444
- Gowdy333
当它应该是3个错误,因为史密斯不包含数字。
有没有人对此提出建议?
答案 0 :(得分:1)
您还有一个逻辑错误:
if (Regex.IsMatch(csv[i].ToString(), "[a-zA-Z]", RegexOptions.IgnoreCase)) //REGEX letters only min of 5 char max of 20
应该是
if (!Regex.IsMatch(csv[i].ToString(), "^[a-zA-Z]+$", RegexOptions.IgnoreCase)) //REGEX letters only min of 5 char max of 20
因为如果名称中包含其他字符而不是[a-zA-Z]
,那只是一个错误,对吗?
(如果您使用RegexOptions.IgnoreCase
,则不需要[a-zA-Z]
,[a-z]
会这样做
答案 1 :(得分:0)
您需要为正则表达式添加单词边界,或者启动“^”并结束“$”
即
^[a-zA-Z]+$
您当前的正则表达式不正确,并且会匹配任何位置包含a-z或A-Z,任何字母的任何字符串。