因为正则表达式出错了

时间:2016-05-31 20:02:20

标签: c# regex ssis

对于我为工作而做的其中一个项目,我必须制作一个报告,从SSIS脚本组件中获取一个主管列表。不幸的是,这一功能给了我一些问题,我无法弄清楚原因。下面的函数从SSIS输入缓冲区获取一行,并检查一个名为supervisors的列表变量,以查看该实例中的名称是否已经在名为supervisors的列表中,如果没有,则将该名称从输入缓冲区行中放入名单。出于某种原因,它只是添加了通过输入缓冲区的所有人,我真的不知道为什么!

例如,我必须有一份员工名单及其主管姓名:

  • Fred Flintstones |饼干怪物
  • john Doe |苦力怪物
  • willy wonka | rubber duckie

我想得到的结果就像是一个由...组成的怪物,橡皮鸭,

相反,我得到了这个......饼干怪物,饼干怪物,橡皮鸭

    public void check(Input0Buffer Row)
{
    bool flag = true;
    string expression = String.Format("({0}|{1})", Row.FirstName, Row.LastName);
    Regex lookup = new Regex(expression,RegexOptions.IgnoreCase);
    if (supervisors.Count > 0)
    {
        foreach (string person in supervisors)
        {
            if (lookup.Matches(person).Count >= 2)
            {
                flag = false;
                break;
            }

        }
        if (flag)
            supervisors.Add(Row.ReportsToName);
    }
    else
        supervisors.Add(Row.ReportsToName);
}

以下是SSIS脚本中的所有代码。

        using System;
        using System.Data;
        using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
        using Microsoft.SqlServer.Dts.Runtime.Wrapper;
        using System.IO;
        using System.Collections.Generic;
        using System.Text.RegularExpressions;


        /// <summary>
        /// This is the class to which to add your code.  Do not change the name, attributes, or parent
        /// of this class.
        /// </summary>
        [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
        public class ScriptMain : UserComponent
        {
            public List<Dictionary<string, string>> Employees;
            public List<string> supervisors;
            string temp;
            /// <summary>
            /// This method is called once, before rows begin to be processed in the data flow.
            ///
            /// You can remove this method if you don't need to do anything here.
            /// </summary>
            public override void PreExecute()
            {
                base.PreExecute();
                 Employees = new List<Dictionary<string, string>>();
                 supervisors = new List<string>();
                 temp = "First Name\tLast Name\tEXT\tUsername\n";
            }

            /// <summary>
            /// This method is called after all the rows have passed through this component.
            ///
            /// You can delete this method if you don't need to do anything here.
            /// </summary>
            public override void PostExecute()
            {
                base.PostExecute();
                foreach (string supervisor in supervisors)
                {
                    foreach (Dictionary<string, string> employee in Employees)
                    {
                        if(match(supervisor,employee))
                        {
                            temp+= String.Format("{0}\t{1}\t{2}\t{3}\n",employee["First"],employee["last"],employee["EXT"],employee["Username"]);
                            break;
                        }
                    }
                }
                File.WriteAllLines(@"blahvlajsdlfh", (string[])temp.Split('\n'));
                File.AppendAllLines(@"Q:gfdhdfghdfgh", supervisors.ToArray());
            }

            /// <summary>
            /// This method is called once for every row that passes through the component from Input0.
            ///
            /// Example of reading a value from a column in the the row:
            ///  string zipCode = Row.ZipCode
            ///
            /// Example of writing a value to a column in the row:
            ///  Row.ZipCode = zipCode
            /// </summary>
            /// <param name="Row">The row that is currently passing through the component</param>
            public override void Input0_ProcessInputRow(Input0Buffer Row)
            {
                Dictionary<string, string> temp_dict = new Dictionary<string, string>();
                temp_dict.Add("First", Row.FirstName);
                temp_dict.Add("last", Row.LastName);
                if (Row.ext_IsNull)
                    temp_dict.Add("EXT", "0000");
                else
                    temp_dict.Add("EXT", Row.ext);
                if (Row.OEEUSERNAME_IsNull)
                    temp_dict.Add("Username", "not available");
                else
                    temp_dict.Add("Username", Row.OEEUSERNAME);
                Employees.Add(temp_dict);
                check(Row);   
            }
            /// <summary>
            /// checks to see if a person is the supervisor list.
            /// </summary>
            /// <param name="Row"></param>
            public void check(Input0Buffer Row)
            {
                bool flag = true;
                string expression = String.Format("({0}|{1})", Row.FirstName, Row.LastName);
                Regex lookup = new Regex(expression,RegexOptions.IgnoreCase);
                if (supervisors.Count > 0)
                {
                    foreach (string person in supervisors)
                    {
                        if (lookup.Matches(person).Count >= 2)
                        {
                            flag = false;
                            break;
                        }

                    }
                    if (flag)
                        supervisors.Add(Row.ReportsToName);
                }
                else
                    supervisors.Add(Row.ReportsToName);
            }
            /// <summary>
            /// used to match supervisors to all of there information
            /// </summary>
            /// <param name="supervisor"></param>
            /// <param name="employees"></param>
            /// <returns></returns>
            public bool match(string supervisor, Dictionary<string, string> employees)
            {
                string expression = String.Format("({0}|{1})", employees["First"], employees["last"]);
                Regex lookup = new Regex(expression, RegexOptions.IgnoreCase);
                if (lookup.Matches(supervisor).Count >= 2)
                    return true;
                return false;
            }


        }

1 个答案:

答案 0 :(得分:0)

事实证明我指的是错误的变量。

此... string expression = String.Format("({0}|{1})", Row.FirstName, Row.LastName);

应该是这个...... string expression = Row.Reporttoo;