//one "(283),(284),B(285);B(286),(287),(288)"; 输出结果是: 283-284,B285;B286,287-288
//two "(283),(284),(285);B(286),(287),(288)"; 输出结果是:283-285;B286,287-288
//three "(283),B(284),B(285);B(286),(287),(288)"; 输出结果是:283,B284-B285;B286,287-288
//four "B(283),B(284),B(285);B(286),(287),(288)"; 输出结果是:B283-B285;B286,287-288// var splitBox = "A(1);A(1)";输出结果是:A(1);A(1)
//"A(28),A(29),A(53),B(11), (12),C(1),C(2),鸟栖(A4),鸟栖(A5),(283),(284),B(285);B(286),";
/// <summary> /// 返回解析后的箱号 /// </summary> /// <param name="saveContainsLetter">包含开头字母的数据</param> /// <param name="saveLetter">开头字母</param> /// <param name="saveNoContainsLetter">不包含字母的数据</param> /// <returns></returns> private string GetReturnSplitBoxNo(string saveContainsLetter, string saveLetter, string saveNoContainsLetter) { //返回解析后的结果 var retrnResultBox = string.Empty; //不包含字母的数据 var vNoContainsLetter = saveNoContainsLetter == "" ? "" : GetBoxNumber(saveNoContainsLetter) + ","; //包含开头字母的数据 var vContainsLetter = string.Empty; if (!string.IsNullOrEmpty(saveLetter) && !string.IsNullOrEmpty(saveContainsLetter)) { if (GetBoxNumber(saveContainsLetter).Contains('-')) { //1-6,8-13,7 var splitContains = GetBoxNumber(saveContainsLetter).Split(',').Where(x => !string.IsNullOrEmpty(x)); foreach (var item in splitContains) { //判断横杠-后面是否为空 if (item.Contains('-')) { var splitLineOne = item.Split('-')[1] == "" ? "" : item.Split('-')[1]; vContainsLetter += saveLetter + item.Split('-')[0] + "-" + saveLetter + splitLineOne + ","; } else { vContainsLetter += saveLetter + item + ","; } } } else { foreach (var item in saveContainsLetter.Split(',').Where(x => !string.IsNullOrEmpty(x))) { vContainsLetter += saveLetter + item + ","; } //vContainsLetter = saveContainsLetter == "" ? "" : saveContainsLetter; //vContainsLetter = saveLetter + vContainsLetter; } } retrnResultBox += vNoContainsLetter + vContainsLetter + ";"; return retrnResultBox; } private void button5_Click(object sender, EventArgs e) { //存放解析split的结果并返回最终结果 var returnBoxNo = string.Empty; //保存开头不含字母 var saveNoContainsLetter = string.Empty; //保存开头包含字母 var saveContainsLetter = string.Empty; //保存开头字母 var saveLetter = string.Empty; List<string> lstSaveBoxNo = new List<string>(); //开头两个字母数据 var twoLetter = string.Empty; //开头两个字母 var twosaveLetter = string.Empty; //one "(283),(284),B(285);B(286),(287),(288)"; 283-284,B285;B286,287-288 //two "(283),(284),(285);B(286),(287),(288)"; 283-285;B286,287-288 //three "(283),B(284),B(285);B(286),(287),(288)"; 283,B284-B285;B286,287-288 //FOUR "B(283),B(284),B(285);B(286),(287),(288)"; B283-B285;B286,287-288 var splitBox = "A(1);A(1)";//"(283),(284),B(285),B(286),B(287),B(288)";//"C(415),C(416),C(417),C(418),C(419),C(420),C(421),C(422),C(423),C(424),C(425),C(426),C(427),C(428),C(429),C(430),C(431),C(432),C(433),C(434),C(248),C(249),C(250),C(251),C(252),C(253),C(254),C(255),C(256),C(257),C(258),C(259),C(260),C(261),C(262),C(263),C(264),C(312),C(313),C(314),C(315),C(316),C(317),C(318),C(319),C(320),C(321),C(322),C(323),C(324),C(325),C(326),C(327),C(328),C(329),C(330),C(331),C(332),C(333),C(334),C(335),C(336),C(337),C(338),C(339),C(340),C(341),C(342),C(343),C(344),C(345),C(346),C(347),C(348),C(349),C(350),C(351),C(352),C(353),C(354),C(287),C(288),C(289),C(290),C(291),C(292),C(293),C(294),C(295),C(296),C(297),C(298),C(299),C(300),C(301),C(302),C(303),C(304),C(305),C(306),C(307),C(308),C(309),C(310),C(311),C(435),C(436),C(437),C(438),C(439),C(440),C(441),C(442),C(443),C(444),C(445),C(446),C(447),C(448),C(449),C(450),C(451),C(452),C(453),C(454),C(455),C(456),C(457),C(458),C(459),C(460),C(461),C(462),C(463),C(464),C(465),C(466),C(467),C(468),C(469),CA(1),CA(2),CA(3),CA(4),CA(5),CA(6),CA(8),CA(9),CA(10),CA(11),CA(12),CA(13),CA(7),C(657),C(658),C(659),C(660),C(661),C(662),C(663),C(664),C(665),C(666),C(667),C(668),C(669),C(670),C(671),C(672),C(673),C(674),C(675),C(614),C(615),C(618),C(619),C(620),C(621),C(622),C(623),C(624),C(625),C(626),C(628),C(629),C(630),C(631),C(632),C(633),C(634),C(635),C(636),C(637),C(638),C(644),C(645),C(646),C(647),C(648),C(649),C(650),C(651),C(652),C(653),C(616),C(655),C(656),C(590),C(591),C(592),C(593),C(594),C(595),C(596),C(597),C(598),C(599),C(600),C(601),C(602),C(603),C(604),C(605),C(606),C(607),C(608),C(609),C(610),C(611),C(612),C(613),C(470),C(471),C(472),C(473),C(474),C(475),C(476),C(477),C(478),C(479),C(480),C(481),C(482),C(483),C(484),C(485),C(486),C(487),C(488),C(489),C(490),C(491),C(492),C(493),C(494),C(495),C(496),C(497),C(498),C(499),C(500),C(501),C(502),C(503),C(504),C(505),C(506),C(507),C(508),C(509),C(510),C(511),C(512),C(513),C(514),C(515),C(516),C(517),C(518),C(519),C(520),C(521),C(522),C(523),C(524),C(525),C(526),C(527),C(528),C(529),C(150),C(195),C(196),C(197),C(198),C(199),C(200),C(201),C(202),C(203),C(204),C(205),C(206),C(207),C(208),C(209),C(210),C(211),C(212),C(213),C(214),C(215),C(216),C(217),C(218),C(219),C(220),C(221),C(222),C(223),C(224),C(225),C(226),C(227),C(228),C(229),C(230),C(231),C(265),C(266),C(267),C(268),C(269),C(270),C(271),C(272),C(273),C(274),C(275),C(276),C(277),C(278),C(279),C(280),C(281),C(282),C(283),C(284),C(285),C(286),C(151),C(152),C(153),C(154),C(155),C(156),C(157),C(158),C(159),C(160),C(161),C(162),C(163),C(164),C(165),C(166),C(167),C(168),C(169),C(170),C(171),C(172),C(173),C(174),C(175),C(176),C(177),C(178),C(179),C(180),C(181),C(182),C(183),C(184),C(185),C(186),C(187),C(188),C(189),C(190),C(191),C(192),C(193),C(194),C(232),C(233),C(234),C(235),C(236),C(237),C(238),C(239),C(240),C(241),C(242),C(243),C(244),C(245),C(246),C(247),C(1),C(2),C(3),C(4),C(5),C(6),C(7),C(8),C(9),C(10),C(11),C(12),C(13),C(14),C(15),C(16),C(17),C(18),C(19),C(20),C(21),C(22),C(23),C(24),C(25),C(26),C(27),C(28),C(29),C(30),C(31),C(32),C(33),C(34),C(35),C(36),C(37),C(38),C(39),C(40),C(41),C(42),C(43),C(44),C(45),C(46),C(47),C(48),C(49),C(50),C(51),C(52),C(53),C(54),C(55),C(56),C(57),C(58),C(59),C(60),C(61),C(62),C(63),C(64),C(65),C(66),C(67),C(68),C(69),C(70),C(71),C(72),C(73),C(74),C(75),C(76),C(77),C(78),C(79),C(80),C(81),C(82),C(83),C(84),C(85),C(86),C(87),C(88),C(89),C(90),C(91),C(92),C(93),C(94),C(355),C(356),C(357),C(358),C(359),C(360),C(361),C(362),C(363),C(364),C(365),C(366),C(367),C(368),C(369),C(370),C(371),C(372),C(373),C(374),C(375),C(376),C(377),C(378),C(379),C(380),C(381),C(382),C(383),C(384),C(385),C(386),C(387),C(388),C(389),C(390),C(391),C(392),C(393),C(394),C(395),C(396),C(397),C(398),C(399),C(400),C(401),C(402),C(403),C(404),C(405),C(406),C(407),C(408),C(409),C(410),C(411),C(412),C(413),C(414),C(617),C(627),C(639),C(640),C(641),C(642),C(643),C(654),C(95),C(96),C(97),C(98),C(99),C(100),C(101),C(102),C(103),C(104),C(105),C(106),C(107),C(108),C(109),C(110),C(111),C(112),C(113),C(114),C(115),C(116),C(117),C(118),C(119),C(120),C(121),C(122),C(123),C(124),C(125),C(126),C(127),C(128),C(129),C(130),C(131),C(132),C(133),C(134),C(135),C(136),C(137),C(138),C(139),C(140),C(141),C(142),C(143),C(144),C(145),C(146),C(147),C(148),C(149),C(530),C(531),C(532),C(533),C(534),C(535),C(536),C(537),C(538),C(539),C(540),C(541),C(542),C(543),C(544),C(545),C(546),C(547),C(548),C(549),C(550),C(551),C(552),C(553),C(554),C(555),C(556),C(557),C(558),C(559),C(560),C(561),C(562),C(563),C(564),C(565),C(566),C(567),C(568),C(569),C(570),C(571),C(572),C(573),C(574),C(575),C(576),C(577),C(578),C(579),C(580),C(581),C(582),C(583),C(584),C(585),C(586),C(587),C(588),C(589),"; //"B(283),B(284),B(285),B(286),B(287),B(288)";//"CA(1),CA(2),CA(3),CA(4),CA(5),CA(6),CA(8),CA(9),CA(10),CA(11),CA(12),CA(13),CA(7),";//"B(283),B(284),B(285),B(286),B(287),B(288)"; if (strBoxNo.Contains(';')) { //拆分多个品番的箱号 foreach (var item in strBoxNo.Split(';')) { //拆分分号前面的品番-箱号 var splitPinfan = item.ToString().Split(',').Where(x => !string.IsNullOrEmpty(x)); foreach (var splititem in splitPinfan) { //判断箱号中带有字母时突然出现不带字母的处理 if (!Regex.IsMatch(splititem, "[a-zA-Z\u4e00-\u9fa5]")) //"[a-zA-Z]")) { lstSaveBoxNo.Add(splititem.Split('(', ')')[1]); } //包含字母的处理 else { lstSaveBoxNo.Add(splititem.Replace("(", "").Replace(")", "")); } } foreach (var splititem in lstSaveBoxNo) { //判断箱号中带有字母时突然出现不带字母的处理 if (!Regex.IsMatch(splititem, "[a-zA-Z\u4e00-\u9fa5]"))//"[a-zA-Z]")) { saveNoContainsLetter += splititem.Replace("(", "").Replace(")", "") + ","; } //开头一个带字母 else if (splititem.Substring(0, 1).Length >= 1 && Regex.IsMatch(splititem.Substring(0, 1), "[a-zA-Z]")) { saveContainsLetter += splititem.Substring(1, splititem.Length - 1) + ","; saveLetter = splititem.Substring(0, 1); } //开头2个带字母 else if (splititem.Substring(0, 2).Length >= 2 && Regex.IsMatch(splititem.Substring(0, 2), "[a-zA-Z]")) { twoLetter += splititem.Substring(2, splititem.Length - splititem.Substring(0, 2).Length) + ","; twosaveLetter = splititem.Substring(0, 2); } //前两位是汉字第三位是数字时 else if (splititem.Substring(0, 2).Length >= 2 && Regex.IsMatch(splititem.Substring(0, 2), "[\u4e00-\u9fa5]") && Regex.IsMatch(splititem.Substring(0, 3), "[0-9]")) { twoLetter += splititem.Substring(2, splititem.Length - splititem.Substring(0, 2).Length) + ","; twosaveLetter = splititem.Substring(0, 2); } //开头3个带字母 else if (splititem.Substring(0, 3).Length >= 3 && Regex.IsMatch(splititem.Substring(0, 3), "[a-zA-Z\u4e00-\u9fa5]")) { twoLetter += splititem.Substring(3, splititem.Length - splititem.Substring(0, 3).Length) + ","; twosaveLetter = splititem.Substring(0, 3); } } //开头两个带字母处理 if (!string.IsNullOrEmpty(twoLetter) && !string.IsNullOrEmpty(twosaveLetter)) { returnBoxNo += GetReturnSplitBoxNo(twoLetter, twosaveLetter, ""); } else { returnBoxNo += GetReturnSplitBoxNo(saveContainsLetter, saveLetter, saveNoContainsLetter); } saveNoContainsLetter = string.Empty; saveContainsLetter = string.Empty; saveLetter = string.Empty; twoLetter = string.Empty; twosaveLetter = string.Empty; lstSaveBoxNo.Clear(); } } else { //拆分单个品番的箱号 var splitPinfan = strBoxNo.ToString().Split(',').Where(x => !string.IsNullOrEmpty(x)); foreach (var splititem in splitPinfan) { //判断箱号中带有字母时突然出现不带字母的处理 if (!Regex.IsMatch(splititem, "[a-zA-Z\u4e00-\u9fa5]")) { lstSaveBoxNo.Add(splititem.Split('(', ')')[1]); } //包含字母的处理 else { lstSaveBoxNo.Add(splititem.Replace("(", "").Replace(")", "")); } } foreach (var splititem in lstSaveBoxNo) { //判断箱号中带有字母时突然出现不带字母的处理 if (!Regex.IsMatch(splititem, "[a-zA-Z\u4e00-\u9fa5]")) { saveNoContainsLetter += splititem.Replace("(", "").Replace(")", "") + ","; } //开头一个带字母 else if (splititem.Substring(0, 1).Length >= 1 && Regex.IsMatch(splititem.Substring(0, 1), "[a-zA-Z]")) { saveContainsLetter += splititem.Substring(1, splititem.Length - 1) + ","; saveLetter = splititem.Substring(0, 1); } //开头2个带字母 else if (splititem.Substring(0, 2).Length >= 2 && Regex.IsMatch(splititem.Substring(0, 2), "[a-zA-Z]")) { twoLetter += splititem.Substring(2, splititem.Length - splititem.Substring(0, 2).Length) + ","; twosaveLetter = splititem.Substring(0, 2); } //前两位是汉字第三位是数字时 else if (splititem.Substring(0, 2).Length >= 2 && Regex.IsMatch(splititem.Substring(0, 2), "[\u4e00-\u9fa5]")&& Regex.IsMatch(splititem.Substring(0, 3), "[0-9]")) { twoLetter += splititem.Substring(2, splititem.Length - splititem.Substring(0, 2).Length) + ","; twosaveLetter = splititem.Substring(0, 2); } //开头3个带字母 else if (splititem.Substring(0, 3).Length >= 3 && Regex.IsMatch(splititem.Substring(0, 3), "[a-zA-Z\u4e00-\u9fa5]")) { twoLetter += splititem.Substring(3, splititem.Length - splititem.Substring(0, 3).Length) + ","; twosaveLetter = splititem.Substring(0, 3); } } //开头两个或者三个带字母处理 if (!string.IsNullOrEmpty(twoLetter) && !string.IsNullOrEmpty(twosaveLetter)) { returnBoxNo += GetReturnSplitBoxNo(twoLetter, twosaveLetter, ""); } else { returnBoxNo += GetReturnSplitBoxNo(saveContainsLetter, saveLetter, saveNoContainsLetter); } saveNoContainsLetter = string.Empty; saveContainsLetter = string.Empty; saveLetter = string.Empty; twoLetter = string.Empty; twosaveLetter = string.Empty; lstSaveBoxNo.Clear(); } strBoxNo = returnBoxNo.Replace(",;;", ";").ToString(); return strBoxNo.Substring(0, strBoxNo.Length - 1); } //判断数据是否连贯。是否中断 public string GetBoxNumber(string strBoxNoList) { var items = strBoxNoList.Split(','); List<string> result = new List<string>(); int curNum = 0; int? prevNum = null; foreach (string item in items) { if (!int.TryParse(item, out curNum)) continue; if (prevNum == null) { prevNum = curNum; result.Add(item); continue; } if (curNum - prevNum == 1) result[result.Count - 1] += "," + item; else result.Add(item); prevNum = curNum; } var strBoxNo = string.Empty; foreach (var item in result) { var MaxMin = item.Split(',').Where(w => !string.IsNullOrEmpty(w.Trim())).Select (p => Int32.Parse(p)); if (item.Length > 2) { //update if (item.Contains(',')) strBoxNo += MaxMin.Min() + "-" + MaxMin.Max() + ","; else strBoxNo += MaxMin.First() + ","; //strBoxNo += MaxMin.Min() + "-" + MaxMin.Max() + ","; } else { strBoxNo += MaxMin.First() + ","; } } return strBoxNo.Substring(0, strBoxNo.Length - 1); }