用逗号分割忽略特定的逗号C#
split by comma ignoring specific commas C#
我有一个看起来像这样的字符串:
NULL VALUE,25,000-30,000,31,000-32,000,33,000-50,000
我怎样才能拆分它以便获得:
NULL VALUE or 25,000-30,000 or 31,000-32,000 or 33,000-50,000
我尝试使用正则表达式进行拆分,但没有成功,它 returns
NULL VALUE
25
000-30
000
31
000-32
000
33
000-50
000
我试过这样 Regex.Split(items[1], ",(?=(?:[^']*'[^']*')*[^']*$)")
但它 return 不是我需要的。
编者注:摘自评论
如果值为千值,则不得以逗号分隔,否则应以逗号分隔,即
NULL VALUE,25,000-30,000
应该是 NULL VALUE | 25,000 - 30,000
尝试像这样进行解析是一场噩梦,您可能永远无法做到 100% 无错误。如果可能,请尝试使用适当的分隔符获取值。
static void Main(string[] args)
{
string x = "NULL VALUE,25,000-30,000,31,000-32,000,33,000-50,000";
string regexstring = @"([A-Z]{1,} [A-Z]{1,}|\d{1,},\d{3}-\d{1,},\d{3}|\d{1,3}-\d{1,3}|\d{1,},\d{3}-\d{1,3}|\d{1,3}-\d{1,},\d{3})";
var erg = Regex.Split(x, regexstring);
foreach (var item in erg)
{
Console.WriteLine(item);
}
}
它产生:
NULL VALUE
,
25,000-30,000
,
31,000-32,000
,
33,000-50,000
此解决方案至少适用于您的测试值。您必须删除空字符串和逗号,但它会正确解析给定的值。
其实你想要的是检测范围,从A到B。A可以是0到X的任意数字,B可以是0到X的任意数字。
鉴于此,您将面临以下情况:
1-10
1-10,000
1,000-10,000
1,000,000-2,000,000
NULL VALUE
找到这些匹配项的模式后,您将能够提取任何范围。
您可以使用此正则表达式:([A-Z ]+|(?:\d{1,3}(?:,\d{3})*)-(?:\d{1,3}(?:,\d{3})*))
表示:
(
[A-Z ]+#NULL VALUE
|#OR
(?:
\d{1,3}#A trailling number
(?:,\d{3})*#Followed or not by a thousand separator and 3 digits
)
-#The range separator
(?:\d{1,3}(?:,\d{3})*)#Same thing here
)
在 C# 代码中,它给出:
var input = "NULL VALUE,25,000-30,000,31,000-32,000,33,000-50,000";
var regex = new Regex(@"(
[A-Z ]+#NULL VALUE
|#OR
(?:
\d{1,3}#A trailling number
(?:,\d{3})*#Followed or not by a thousand separator and 3 digits
)
-#The range separator
(?:\d{1,3}(?:,\d{3})*)#Same thing here
)", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Multiline);
var matches = regex.Matches(input);
foreach (Match match in matches)
{
// Do what you want here, I choose to output it.
Console.WriteLine(match.Groups[1]);
}
输出:
NULL VALUE
25,000-30,000
31,000-32,000
33,000-50,000
我有一个看起来像这样的字符串:
NULL VALUE,25,000-30,000,31,000-32,000,33,000-50,000
我怎样才能拆分它以便获得:
NULL VALUE or 25,000-30,000 or 31,000-32,000 or 33,000-50,000
我尝试使用正则表达式进行拆分,但没有成功,它 returns
NULL VALUE
25
000-30
000
31
000-32
000
33
000-50
000
我试过这样 Regex.Split(items[1], ",(?=(?:[^']*'[^']*')*[^']*$)")
但它 return 不是我需要的。
编者注:摘自评论
如果值为千值,则不得以逗号分隔,否则应以逗号分隔,即
NULL VALUE,25,000-30,000
应该是 NULL VALUE | 25,000 - 30,000
尝试像这样进行解析是一场噩梦,您可能永远无法做到 100% 无错误。如果可能,请尝试使用适当的分隔符获取值。
static void Main(string[] args)
{
string x = "NULL VALUE,25,000-30,000,31,000-32,000,33,000-50,000";
string regexstring = @"([A-Z]{1,} [A-Z]{1,}|\d{1,},\d{3}-\d{1,},\d{3}|\d{1,3}-\d{1,3}|\d{1,},\d{3}-\d{1,3}|\d{1,3}-\d{1,},\d{3})";
var erg = Regex.Split(x, regexstring);
foreach (var item in erg)
{
Console.WriteLine(item);
}
}
它产生:
NULL VALUE
,
25,000-30,000
,
31,000-32,000
,
33,000-50,000
此解决方案至少适用于您的测试值。您必须删除空字符串和逗号,但它会正确解析给定的值。
其实你想要的是检测范围,从A到B。A可以是0到X的任意数字,B可以是0到X的任意数字。
鉴于此,您将面临以下情况:
1-10
1-10,000
1,000-10,000
1,000,000-2,000,000
NULL VALUE
找到这些匹配项的模式后,您将能够提取任何范围。
您可以使用此正则表达式:([A-Z ]+|(?:\d{1,3}(?:,\d{3})*)-(?:\d{1,3}(?:,\d{3})*))
表示:
(
[A-Z ]+#NULL VALUE
|#OR
(?:
\d{1,3}#A trailling number
(?:,\d{3})*#Followed or not by a thousand separator and 3 digits
)
-#The range separator
(?:\d{1,3}(?:,\d{3})*)#Same thing here
)
在 C# 代码中,它给出:
var input = "NULL VALUE,25,000-30,000,31,000-32,000,33,000-50,000";
var regex = new Regex(@"(
[A-Z ]+#NULL VALUE
|#OR
(?:
\d{1,3}#A trailling number
(?:,\d{3})*#Followed or not by a thousand separator and 3 digits
)
-#The range separator
(?:\d{1,3}(?:,\d{3})*)#Same thing here
)", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Multiline);
var matches = regex.Matches(input);
foreach (Match match in matches)
{
// Do what you want here, I choose to output it.
Console.WriteLine(match.Groups[1]);
}
输出:
NULL VALUE
25,000-30,000
31,000-32,000
33,000-50,000