Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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/8/python-3.x/19.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# 需要创建正则表达式以在第一次之后拆分字符串\r\n_C#_Asp.net_Regex - Fatal编程技术网

C# 需要创建正则表达式以在第一次之后拆分字符串\r\n

C# 需要创建正则表达式以在第一次之后拆分字符串\r\n,c#,asp.net,regex,C#,Asp.net,Regex,我陷入了困境 这里有几个输入字符串- "abacuses\r\n25" "alphabet\r\n56,\r\n57" "animals\r\n44,\r\n45,\r\n47" 我需要像这样分割输出- "abacuses\r\n25" to be splitted into A)abacuses B)25 "alphabet\r\n56,\r\n57" to be splitted into A)alphabet B)56,57 "animals\r\n44,\r\n45,\r

我陷入了困境

这里有几个输入字符串-

 "abacuses\r\n25"

"alphabet\r\n56,\r\n57"

"animals\r\n44,\r\n45,\r\n47"
我需要像这样分割输出-

"abacuses\r\n25" to be splitted into A)abacuses B)25


"alphabet\r\n56,\r\n57" to be splitted into A)alphabet B)56,57


"animals\r\n44,\r\n45,\r\n47" to be splitted into A)animals B)44,45,47
到目前为止,我已经尝试过这个,但它不工作-

string[] ina = Regex.Split(indexname, @"\r\n\D+");

string[] ina = Regex.Split(indexname, @"\r\n\");

请帮助您的示例中不需要正则表达式。您可以基本解析字符串:

string input = "animals\r\n44,\r\n45,\r\n47";
var split = input.Split(new char[]{'\r','\n',','}, StringSplitOptions.RemoveEmptyEntries);

var name = split[0];                        //animals
var args = string.Join(",", split.Skip(1)); //44,45,37

许多人使用它进行解析,但Regex不是一种解析语言!它是模式匹配器!它用于在字符串中查找子字符串!如果你可以拆分你的字符串,就去做吧,真的。它比正则表达式更容易理解。

示例中不需要正则表达式。您可以基本解析字符串:

string input = "animals\r\n44,\r\n45,\r\n47";
var split = input.Split(new char[]{'\r','\n',','}, StringSplitOptions.RemoveEmptyEntries);

var name = split[0];                        //animals
var args = string.Join(",", split.Skip(1)); //44,45,37

许多人使用它进行解析,但Regex不是一种解析语言!它是模式匹配器!它用于在字符串中查找子字符串!如果你可以拆分你的字符串,就去做吧,真的。它比正则表达式更容易理解。

如果需要在第一个
\r\n
处拆分字符串,可以使用带有count参数的:

var line = "animals\r\n44,\r\n45,\r\n47";
var res = line
    .Split(new[] {"\r\n"}, 2, StringSplitOptions.RemoveEmptyEntries);
// Demo output
Console.WriteLine(res[0]);
if (res.GetLength(0) > 1) 
    Console.WriteLine(res[1].Replace("\r\n", "")); // In the second value, linebreaks should be removed


.Split中的
2
(新[]{“\r\n”},2,StringSplitOptions.RemoveEmptyEntries)
意味着整个字符串应仅拆分为两部分,由于字符串是从左到右处理的,因此拆分将发生在找到的第一个子字符串上。

如果需要在第一个
\r\n
处拆分字符串,您可以将a与count参数一起使用:

var line = "animals\r\n44,\r\n45,\r\n47";
var res = line
    .Split(new[] {"\r\n"}, 2, StringSplitOptions.RemoveEmptyEntries);
// Demo output
Console.WriteLine(res[0]);
if (res.GetLength(0) > 1) 
    Console.WriteLine(res[1].Replace("\r\n", "")); // In the second value, linebreaks should be removed


.Split中的
2
(新[]{“\r\n”},2,StringSplitOptions.RemoveEmptyEntries)
意味着整个字符串只应拆分为两部分,因为字符串是从左到右处理的,所以拆分将发生在找到的第一个“\r\n”子字符串上。

在这种情况下,您不知道是否删除逗号分隔数字之间的换行符。标题表明这并不重要,但示例非常具体。@eocron06您是一个多么好的救生员。感谢您只需注意:这种方法将导致删除任何独立的
\r
\n
,如果它们出现在
\r\n
之前,则会导致意外结果。我同意这可能不是很重要,但值得注意。当然。这个问题有42种解决方案。我们总共代表了其中的两个。40 ways to go=)在本例中,您不知道是否删除逗号分隔数字之间的换行符。标题表明这并不重要,但示例非常具体。@eocron06您是一个多么好的救生员。感谢您只需注意:这种方法将导致删除任何独立的
\r
\n
,如果它们出现在
\r\n
之前,则会导致意外结果。我同意这可能不是很重要,但值得注意。当然。这个问题有42种解决方案。我们总共代表了其中的两个。40种方法=)好的,那么
“动物\r\n44、\r\n45、树\r\n47”
呢?这是一种可能的输入类型吗?如果是,您希望得到什么样的输出?好的,关于
“动物\r\n44\r\n45,树,\r\n47”
呢?这是一种可能的输入类型吗?如果是,您希望得到什么样的输出?