Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当字母位于数字c之后时,如何拆分字符串#_C#_Regex_String_Split - Fatal编程技术网

C# 当字母位于数字c之后时,如何拆分字符串#

C# 当字母位于数字c之后时,如何拆分字符串#,c#,regex,string,split,C#,Regex,String,Split,当一个字母在一个数字后面时,我想分割我的字符串,但不能去掉数字本身。例如,ABC123CW23F1应输出为ABC123、CW23和F1。我尝试使用String.Spilt string testString = "ABC123CW23F1"; Console.WriteLine(testString); string[] new_String = testString.Split(new char[] { '0', '1', '2', '3' }); for (int i = 0; i <

当一个字母在一个数字后面时,我想分割我的字符串,但不能去掉数字本身。例如,ABC123CW23F1应输出为ABC123、CW23和F1。我尝试使用String.Spilt

string testString = "ABC123CW23F1";
Console.WriteLine(testString);
string[] new_String = testString.Split(new char[] { '0', '1', '2', '3' });
for (int i = 0; i < new_String.Length; i++)
{
     Console.WriteLine(new_String[i]);
}
string testString=“ABC123CW23F1”;
Console.WriteLine(testString);
string[]new_string=testString.Split(新字符[]{'0','1','2','3'});
for(int i=0;i

但是这会输出ABC一些空行,然后CW,然后是一些空行,然后是F。我怎么才能使它不是这样呢?请帮忙。谢谢

您的预期输出表明,只要前面有一个数字,字母继续,您就要进行拆分。您可以在使用lookarounds的以下正则表达式上进行拆分:

(?<=\d)(?=[A-Z])

(?非常优雅!@Sach Regex lookarounds。没有它们不要出门:-)非常感谢。这起作用了。现在我很好奇,是否可以用多个参数来实现这一点。比如说,当一个字母跟在一个数字后面时(就像你做的那样),或者当一个符号(比如“?”)跟在一个字母后面时(比如说),就把它分开?我的代码不需要这个,但现在我想是的,你可以在正则表达式中使用替代。例如:
(?
string str = "A4HM23D9";
string[] split = Regex.Split(str, @"(?<=\d)(?=[A-Z])");
Console.WriteLine(split[0] + " " + split[1] + " " + split[2]);

A4 HM23 D9