我想使用regexp拆分或解析这个但是有一个问题,我需要在解析它时比较它。这是数据字符串
"351856040666992,RA1;2400A1444.5122N12030.5764E026.3150.000.900057031451271113EFA2524018D1430DF90F,,,,,,,,"
我想要的结果是这样的。是的我可以解析前11位数,但下一个我不能解析。
这是我需要的结果。
351856040666992
RA1
2400
A
1444.5122N
12030.5764E
026.3
150.0
00.9
00057
031451
271113
EF
A2
52
40
18D
143
0DF90F
提前感谢所有帮助。
答案 0 :(得分:1)
正则表达式适用于此。转换为c#:
using System.Text.RegularExpressions;
...
string my_string = "351856040666992,RA1;2400A1444.5122N12030.5764E026.3150.000.900057031451271113EFA2524018D1430DF90F,,,,,,,,";
string my_regex = "(.{15})[.,;]*(.{3})[.,;]*(.{4})[.,;]*(.{1})[.,;]*(.{10})[.,;]*(.{11})[.,;]*(.{5})[.,;]*(.{5})[.,;]*(.{4})[.,;]*(.{5})[.,;]*(.{6})[.,;]*(.{6})[.,;]*(.{2})[.,;]*(.{2})[.,;]*(.{2})[.,;]*(.{2})[.,;]*(.{3})[.,;]*(.{3})[.,;]*(.{6})";
Regex r = new Regex(my_regex);
foreach (Group g in r.Match(my_string).Groups) {
Console.WriteLine(g);
}
打印:
351856040666992
RA1
2400
A
1444.5122N
12030.5764E
026.3
150.0
00.9
00057
031451
271113
EF
A2
52
40
18D
143
0DF90F
请注意,只有 才能运行,如果您要解析的每一行都有相同的字符数。我怀疑这可能不是这种情况,因为1444.5122N
和12030.5764E
具有不同的长度,但似乎具有相同的预期功能。
第二个注意事项:如果在生产代码中使用,请记住在尝试迭代其组之前检查Success
对象的Match
属性。
答案 1 :(得分:0)
您好我找到了办法。这是我的答案,以便帮助其他人。
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
string connetionString = null;
SqlConnection cnn;
//string connectionString = "server=ACER-PC;database=YourDatabaseName;user id=;pwd=";
cnn = new SqlConnection(connetionString);
//string str = "351856040666992,RA1;2400A1444.5122N12030.5764E026.3150.000.900057031451271113EFA2524018D1430DF90F,,,,,,,,";
//string str = "351856040520447,241111;1G,?130903011053,A,0628.9385N,12451.8013E,0.0,71,0.9,00004000;";
string str = "@P,7FC5,117,0,359551035057990,20130905094454,20130905094455,20130905094455,121000369,14507155,0,2,0,8,1,0,0,0,,2000,2000,";
if (str.Length == 105)
{
//FOR ARKNAV(R10-V2) Raw Data Definition
for (int i = 0; i < str.Length; i = i + 8)
{
//IMEI
string imei = str.Substring(i, 15);
//FIRMWARE
string firm = str.Substring(16, 3);
//ACTIVATION STATUS
string actstat = str.Substring(20, 4);
//GPS DATA
string gpsdata = str.Substring(24, 1);
//LAT
string lat = str.Substring(25, 10);
//LONG
string longit = str.Substring(35, 11);
//SPEED ON GROUND
string speedg = str.Substring(46, 5);
//COURSE ON GROUND
string courseg = str.Substring(51, 5);
//HDOP
string hdop = str.Substring(56, 4);
//ALTITUDE
string alt = str.Substring(60, 5);
//TIME
string time = str.Substring(65, 6);
//DATE
string date = str.Substring(71, 6);
//POWER SUPPLY INPUT VOLTAGE
string psupp = str.Substring(77, 2);
//OB Backup batt storage
string obbattstor = str.Substring(79, 2);
//Digital high voltage input status
string dgitalvoltinpstat = str.Substring(81, 2);
//Digital outputs status
string digoutstat = str.Substring(83, 2);
//ANALOG INPUT 1
string aninput1 = str.Substring(85, 3);
//ANALOG INPUT 2
string aninput2 = str.Substring(88, 3);
//KM Run
string kmrun = str.Substring(91, 6);
Console.WriteLine(imei);
Console.WriteLine(firm);
Console.WriteLine(actstat);
Console.WriteLine(gpsdata);
Console.WriteLine(lat);
Console.WriteLine(longit);
Console.WriteLine(speedg);
Console.WriteLine(courseg);
Console.WriteLine(hdop);
Console.WriteLine(alt);
Console.WriteLine(time);
Console.WriteLine(date);
Console.WriteLine(psupp);
Console.WriteLine(obbattstor);
Console.WriteLine(dgitalvoltinpstat);
Console.WriteLine(digoutstat);
Console.WriteLine(aninput1);
Console.WriteLine(aninput2);
Console.WriteLine(kmrun);
Console.ReadLine();
}
}
//FOR ARKNAV(RX8W) Raw Data Definition
else if (str.Length == 85)
{
for (int i = 0; i < str.Length; i = i + 8)
{
//IMEI
string imei = str.Substring(i, 15);
//ProdID
string prodid = str.Substring(16, 6);
//with GPS clock
string clock = str.Substring(23, 2);
//date time
string datetime = str.Substring(27, 12);
//GPS Status
string gpsstat = str.Substring(40, 1);
//LAT
string lat = str.Substring(42, 10);
//LONG
string longit = str.Substring(53, 11);
//Speed
string speed = str.Substring(65, 3);
//Course
string course = str.Substring(69, 2);
//HDOP
string hdop = str.Substring(72, 3);
//Status Code
string statcode = str.Substring(76, 8);
Console.WriteLine(imei);
Console.WriteLine(prodid);
Console.WriteLine(clock);
Console.WriteLine(datetime);
Console.WriteLine(gpsstat);
Console.WriteLine(lat);
Console.WriteLine(longit);
Console.WriteLine(speed);
Console.WriteLine(course);
Console.WriteLine(hdop);
Console.WriteLine(statcode);
Console.ReadLine();
}
}
else if (str.Length == 121)
{
for (int i = 0; i < str.Length; i = i + 8)
{
//Prefix
Console.WriteLine(str.Substring(i, 2));
//CRC Check
Console.WriteLine(str.Substring(3, 4));
//Length of the packet
Console.WriteLine(str.Substring(8, 3));
//sequence ID
Console.WriteLine(str.Substring(12, 1));
//IMEI
Console.WriteLine(str.Substring(14, 15));
//GPS Date Time
Console.WriteLine(str.Substring(30, 14));
//RTC Date Time
Console.WriteLine(str.Substring(45, 14));
//Position Sending Date Time
Console.WriteLine(str.Substring(60, 14));
//Longitude
Console.WriteLine(str.Substring(75, 9));
//Latitude
Console.WriteLine(str.Substring(85, 8));
//Heading
Console.WriteLine(str.Substring(94, 1));
//Report ID
Console.WriteLine(str.Substring(96, 1));
//Odometer
Console.WriteLine(str.Substring(98, 1));
//GPS HDOP
Console.WriteLine(str.Substring(100, 1));
//Input Status
Console.WriteLine(str.Substring(102, 1));
//GPS/VSS Vehicle Speed
Console.WriteLine(str.Substring(104, 1));
//Output Status
Console.WriteLine(str.Substring(106, 1));
//Analog Input Value
Console.WriteLine(str.Substring(108, 1));
//Driver ID
Console.WriteLine(str.Substring(109, 2));
//First Temperature Sensor Value
Console.WriteLine(str.Substring(111, 4));
//Second Temperature Sensor Value
Console.WriteLine(str.Substring(116, 4));
//Text message
Console.ReadLine();
}
}
}
}
}
与此相关,我想补充一个跟进问题。如果我想将存储在变量中的已解析数据插入到MS SQL中,我将在何处以及将插入什么?