[英]Splitting on ; then on =, having issues using String.Split, regex required
"key1"="value1 http://www.example.com?a=1";"key2"="value2 http://www.example.com?a=2";
I need to split the above line 2 times, the first time it is the comma character ; 我需要将上面的行拆分两次,第一次是逗号字符; and the second time on the = sign. 和第二次在=号上。
It doesn't work correctly because the value part has the = sign in it also. 由于值部分也带有=符号,因此无法正常工作。
My code doesn't work as it was assuming the value part doesnt' have an = sign in it, and it isn't using regex simply String.Split('='). 我的代码不起作用,因为它假定值部分中没有=符号,并且它没有简单地使用正则表达式String.Split('=')。
Can someone help with the regex required, I added double quotes around both the key/value to help keep things seperate. 有人可以帮忙解决所需的正则表达式吗?我在键/值两边都加了双引号,以使事情分开。
I didn't use a regex, but you could do something like the following: 我没有使用正则表达式,但是您可以执行以下操作:
string test =@"""key1""=""value1 http://www.example.com?a=1"";""key2""=""value2 http://www.example.com?a=2""";
string[] arr = test.Split(';');
foreach (string s in arr)
{
int index = s.IndexOf('=');
string key = s.Substring(0, index);
string value = s.Substring(index+1, s.Length - index);
}
Use the String.Split(char[], int)
overload (http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx). 使用String.Split(char[], int)
重载(http://msdn.microsoft.com/zh-cn/library/c1bs0eda.aspx)。 The second parameter will limit the number of substrings to return. 第二个参数将限制要返回的子字符串的数量。 If you know your strings will always have at least 1 equal sign (key/value pairs), then set the second parameter to 2. 如果您知道您的字符串将始终至少包含1个等号(键/值对),则将第二个参数设置为2。
string x = "key1=value1 http://www.example.com?a=1;key2=value2 http://www.example.com?a=2;";
char[] equal = new char[1] { '=' };
char[] semi = new char[1] { ';' };
string[] list = x.Split(semi, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in list)
{
string[] kvp = s.Split(equal, 2);
Console.WriteLine("Key: {0}, Value: {1}", kvp[0], kvp[1]);
}
- Result: -结果:
Key: key1, Value: value1 http://www.example.com?a=1
Key: key2, Value: value2 http://www.example.com?a=2
Well what you can do, is you can use IndexOf
to get the index of the first =
好吧,您可以使用IndexOf
来获取第一个=
的索引
int i = myStr.IndexOf('=');
and then you can use the String.Substring
to get the key and value 然后可以使用String.Substring
来获取键和值
string key = myStr.Substring(0, i)
string value = myStr.SubString(i+1);
Here is some documentation on the String Class that you might find useful 这是一些有关字符串类的文档,您可能会觉得有用
You need to match not split the text 您需要匹配而不拆分文本
var keys= Regex.Matches(yourString,"""(.*?)""=.*?(http.*?)"";").Cast<Match>().Select(x=>
new
{
key=x.Groups[1].Value,
value=x.Groups[2].Value
}
);
foreach(key in keys)
{
key.key;//the key value
key.value;//the value
}
Your regex should look like this. 您的正则表达式应如下所示。
"(.+?)"="(.+?)"
Sadly I do not know C#
but this should work in every language. 不幸的是,我不懂C#
但是这应该适用于每种语言。 To get the results you have to select for every match: 要获得结果,您必须为每个匹配项选择一个:
group(1) as keys
group(2) as values
Use String.Split
with StringSplitOptions.RemoveEmptyEntries
and an array of strings with delimiters 将String.Split
与StringSplitOptions.RemoveEmptyEntries
使用,并使用带有定界符的字符串数组
string s = "\"key1\"=\"value1 http://www.example.com?a=1\";\"key2\"=\"value2 http://www.example.com?a=2\"";
string[] result = s.Split(new string[] { "\";\"", "\"=\"", "\"" },
StringSplitOptions.RemoveEmptyEntries);
result = {string[4]} [0]: "key1" [1]: "value1 http://www.example.com?a=1" [2]: "key2" [3]: "value2 http://www.example.com?a=2"
I use the following delimiters (including the double quotes): 我使用以下定界符(包括双引号):
";"
"="
"
You could also try using the Split method with multiple tokens here this will give you a string[] of multiple values that were split out based on your tokens 您也可以尝试将Split方法与多个标记一起使用,这将为您提供一个基于多个标记分割的多个值的string []
if you want to remove empty Entries you could also do the code like this 如果要删除空条目,也可以执行以下代码
string strValue = @"""key1""=""value1 http://www.example.com?a=1"";""key2""=""value2 http://www.example.com?a=2""";
string[] strSplit = strValue.Split(new string[] { "\";\"", "\"=\"", "\"" }, StringSplitOptions.RemoveEmptyEntries);
Results 结果
strSplit {string[4]} string[]
[0] "key1"
[1] "value1 http://www.example.com?a=1"
[2] "key2"
[3] "value2 http://www.example.com?a=2"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.