使用匹配正则表达式时没有输出

时间:2014-01-20 21:51:23

标签: c# regex match

Match m = Regex.Match之后我没有输出。我认为它存在问题。有人可以检查代码,如果可以的话。

输入alarm_text如下:

  

Prio 1 WESTERSTRAAT 140 ENKHUIZEN Woning brand 4632 4651

我想要做的是将字符串分为四组:“Prio 1”,“WESTERSTRAAT 140 ENKHUIZEN”,“Woning brand”,“4632 4651”

 using System;
 using System.Text.RegularExpressions;
 using System.Data.Odbc;

 namespace pdw_db
 {
    internal class pdw_db
    {
        public pdw_db()
        {
        }

        private static void databaseThis(string capcode, string melding, string korps, string streetName, string preText, string postText, string cars)
        {
                string[] strArray = new string[] { "insert into alarmeringen2010(capcode, desc1, department, melding, alerting, data1, voertuigen)values('", capcode, "','", melding, "','", korps, "','", streetName, "','", preText, "','", postText, "','", cars, "',);" };
                string str = string.Concat(strArray);

                OdbcConnection odbcConnection = new OdbcConnection("DSN=pdw_db");
                try
                {
                    OdbcCommand odbcCommand = new OdbcCommand(str, odbcConnection);
                    odbcConnection.Open();
                    OdbcDataReader odbcDataReader = odbcCommand.ExecuteReader();
                    while (odbcDataReader.Read())
                    {
                        Console.WriteLine(odbcDataReader.GetString(0));
                    }
                    odbcDataReader.Close();
                }
                finally
                {
                    if (odbcConnection != null)
                    {
                        odbcConnection.Dispose();
                    }
                }

        }

        private static void Main(string[] args)
        {
            if (args.Length > 2)
            {
                return;
            }
            else
            {
                string alarm_capcode, alarm_text, alarm_korps;

                //kill qoutes in alarmering
                args[1] = args[1].Replace("\"", "");
                args[1] = args[1].Replace("\'", "");
                args[1] = args[1].Replace("'", "");
                args[2] = args[2].Replace("\"", "");
                args[2] = args[2].Replace("\'", "");
                args[2] = args[2].Replace("'", "");

                alarm_capcode = args[0];
                alarm_text = args[1];
                alarm_korps = args[2];

                Match m = Regex.Match(alarm_text, @"(.*\s)([A-Z]+.[0-9]+.[A-Z])(.*\s)([0-9].*\s)");

                string preText = m.Groups[1].Value;
                string streetName = m.Groups[2].Value;
                string postText = m.Groups[3].Value;
                string cars = m.Groups[4].Value;

                databaseThis(alarm_capcode, alarm_text, alarm_korps, streetName, preText, postText, cars);

            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

问题在于您的第4组 - 没有[0-9]+[0-9].*您的上一组缺少您的最后一个组:

Match m = Regex.Match(alarm_text, @"(.*\s)([A-Z]+.[0-9]+.[A-Z])(.*\s)([0-9].*\s[0-9]+)");

当然,这也取决于你最后一组中是否只有数字 第二组中也有错误 - 没有+\s文本转到第3组 - 现在应该没问题

Match m = Regex.Match(alarm_text, @"(.*\s)([A-Z]+.[0-9]+.[A-Z]+\s)(.*\s)([0-9].*\s[0-9].*)");

答案 1 :(得分:0)

让代码几乎正常工作。只有他正在采取平面价值文本。不是字符串

private static void databaseThis(string capcode, string melding, string korps)
        {
            Match m = Regex.Match(melding, @"(.*\s)([A-Z]+.[0-9]+.[A-Z]+\s)(.*\s)([0-9].*\s[0-9].*)");
            string preText = m.Groups[1].Value;
            string streetName = m.Groups[2].Value;
            string postText = m.Groups[3].Value;
            string cars = m.Groups[4].Value;

            string[] strArray = new string[] { "insert into alarmeringen2010(capcode, desc1, department, melding, alerting, data1, voertuigen)values('capcode','melding','korps','streetName','preText','postText','cars');" };
            string str = string.Concat(strArray);
          }