How can I take the string foo[]=1&foo[]=5&foo[]=2
and return a collection with the values 1,5,2
in that order. I am looking for an answer using regex in C#. Thanks
如何取字符串foo [] = 1&foo [] = 5&foo [] = 2并按顺序返回值为1,5,2的集合。我在C#中使用正则表达式寻找答案。谢谢
8 个解决方案
#1
3
In C# you can use capturing groups
在C#中,您可以使用捕获组
private void RegexTest()
{
String input = "foo[]=1&foo[]=5&foo[]=2";
String pattern = @"foo\[\]=(\d+)";
Regex regex = new Regex(pattern);
foreach (Match match in regex.Matches(input))
{
Console.Out.WriteLine(match.Groups[1]);
}
}
#2
1
I don't know C#, but...
我不知道C#,但......
In java:
String[] nums = String.split(yourString, "&?foo[]");
The second argument in the String.split()
method is a regex telling the method where to split the String.
String.split()方法中的第二个参数是一个正则表达式,告诉方法在哪里拆分String。
#3
1
I'd use this particular pattern:
我会用这个特殊的模式:
string re = @"foo\[\]=(?<value>\d+)";
So something like (not tested):
所以像(未经测试):
Regex reValues = new Regex(re,RegexOptions.Compiled);
List<integer> values = new List<integer>();
foreach (Match m in reValues.Matches(...putInputStringHere...)
{
values.Add((int) m.Groups("value").Value);
}
#4
1
Use the Regex.Split() method with an appropriate regex. This will split on parts of the string that match the regular expression and return the results as a string[].
使用Regex.Split()方法和适当的正则表达式。这将拆分与正则表达式匹配的字符串部分,并将结果作为字符串[]返回。
Assuming you want all the values in your querystring without checking if they're numeric, (and without just matching on names like foo[]) you could use this: "&?[^&=]+="
假设您想要查询字符串中的所有值而不检查它们是否为数字(并且不仅仅匹配foo []等名称),您可以使用:“&?[^&=] + =”
string[] values = Regex.Split(“foo[]=1&foo[]=5&foo[]=2”, "&?[^&=]+=");
Incidentally, if you're playing with regular expressions the site http://gskinner.com/RegExr/ is fantastic (I'm just a fan).
顺便说一句,如果你正在使用正则表达式,网站http://gskinner.com/RegExr/太棒了(我只是一个粉丝)。
#5
0
Assuming you're dealing with numbers this pattern should match:
假设你正在处理数字,这个模式应该匹配:
/=(\d+)&?/
#6
0
This should do:
这应该做:
using System.Text.RegularExpressions;
Regex.Replace(s, !@"^[0-9]*$”, "");
Where s is your String where you want the numbers to be extracted.
其中s是您想要提取数字的String。
#7
0
Just make sure to escape the ampersand like so:
只要确保像这样逃避&符号:
/=(\d+)\&/
#8
0
Here's an alternative solution using the built-in string.Split function:
这是使用内置string.Split函数的替代解决方案:
string x = "foo[]=1&foo[]=5&foo[]=2";
string[] separator = new string[2] { "foo[]=", "&" };
string[] vals = x.Split(separator, StringSplitOptions.RemoveEmptyEntries);
#1
3
In C# you can use capturing groups
在C#中,您可以使用捕获组
private void RegexTest()
{
String input = "foo[]=1&foo[]=5&foo[]=2";
String pattern = @"foo\[\]=(\d+)";
Regex regex = new Regex(pattern);
foreach (Match match in regex.Matches(input))
{
Console.Out.WriteLine(match.Groups[1]);
}
}
#2
1
I don't know C#, but...
我不知道C#,但......
In java:
String[] nums = String.split(yourString, "&?foo[]");
The second argument in the String.split()
method is a regex telling the method where to split the String.
String.split()方法中的第二个参数是一个正则表达式,告诉方法在哪里拆分String。
#3
1
I'd use this particular pattern:
我会用这个特殊的模式:
string re = @"foo\[\]=(?<value>\d+)";
So something like (not tested):
所以像(未经测试):
Regex reValues = new Regex(re,RegexOptions.Compiled);
List<integer> values = new List<integer>();
foreach (Match m in reValues.Matches(...putInputStringHere...)
{
values.Add((int) m.Groups("value").Value);
}
#4
1
Use the Regex.Split() method with an appropriate regex. This will split on parts of the string that match the regular expression and return the results as a string[].
使用Regex.Split()方法和适当的正则表达式。这将拆分与正则表达式匹配的字符串部分,并将结果作为字符串[]返回。
Assuming you want all the values in your querystring without checking if they're numeric, (and without just matching on names like foo[]) you could use this: "&?[^&=]+="
假设您想要查询字符串中的所有值而不检查它们是否为数字(并且不仅仅匹配foo []等名称),您可以使用:“&?[^&=] + =”
string[] values = Regex.Split(“foo[]=1&foo[]=5&foo[]=2”, "&?[^&=]+=");
Incidentally, if you're playing with regular expressions the site http://gskinner.com/RegExr/ is fantastic (I'm just a fan).
顺便说一句,如果你正在使用正则表达式,网站http://gskinner.com/RegExr/太棒了(我只是一个粉丝)。
#5
0
Assuming you're dealing with numbers this pattern should match:
假设你正在处理数字,这个模式应该匹配:
/=(\d+)&?/
#6
0
This should do:
这应该做:
using System.Text.RegularExpressions;
Regex.Replace(s, !@"^[0-9]*$”, "");
Where s is your String where you want the numbers to be extracted.
其中s是您想要提取数字的String。
#7
0
Just make sure to escape the ampersand like so:
只要确保像这样逃避&符号:
/=(\d+)\&/
#8
0
Here's an alternative solution using the built-in string.Split function:
这是使用内置string.Split函数的替代解决方案:
string x = "foo[]=1&foo[]=5&foo[]=2";
string[] separator = new string[2] { "foo[]=", "&" };
string[] vals = x.Split(separator, StringSplitOptions.RemoveEmptyEntries);