正则表达式替换
每行的数据格式为:VCBI ZUUU 001A465 KAKID-LEGOS
要求变为格式为:VCBIZUUU001 A465 KAKID-LEGOS 替换掉第一部分和第二部分中间的空格,第二部分和第三部分中间的空格,其他的保持一个空格
string originalString = @"VCBI ZUUU 001 A465 KAKID-LEGOS{0}ZBAA FAOR 001 A465 LEGOS-KAKID{0}ZUUU VCBI 001 A465 LEGOS-KAKID{0}FAOR ZBAA 001 A465 KAKID-LEGOS{0}ZUUU VCBI L01 A465 LEGOS-KAKID{0}VCBI ZUUU L01 A465 KAKID-LEGOS{0}ZUUU VCBI L03 A465 LEGOS-KAKID{0}VCBI ZUUU L03 A465 KAKID-LEGOS"; originalString = string.Format(originalString,Environment.NewLine); Console.WriteLine(@"原字符串:"); Console.WriteLine(originalString); string pattern = @"^([A-Z0-9]*)\s+([A-Z0-9]*)\s+([A-Z0-9]*)\s+([A-Z0-9]*)\s+([A-Z0-9]*)"; Console.WriteLine(@"替换后的字符串"); Console.WriteLine(Regex.Replace(originalString,pattern,"$1$2$3 $4 $5",RegexOptions.Multiline));