Split string with Regex
-
Hi, I need to split a large string and split that splitted string again. I have the following string:
root
{
stringB =
{
"key": "value",
"key": "value",
"StringC":
{
"key": "value",
"key": "value",
"key": "value",
"key": "value",
}
"StringD": "value",
"StringE":
{
"key": "value",
}
}
}The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!
-
Hi, I need to split a large string and split that splitted string again. I have the following string:
root
{
stringB =
{
"key": "value",
"key": "value",
"StringC":
{
"key": "value",
"key": "value",
"key": "value",
"key": "value",
}
"StringD": "value",
"StringE":
{
"key": "value",
}
}
}The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!
That doesn't look regular enough. And try the Regex forum.
-
Hi, I need to split a large string and split that splitted string again. I have the following string:
root
{
stringB =
{
"key": "value",
"key": "value",
"StringC":
{
"key": "value",
"key": "value",
"key": "value",
"key": "value",
}
"StringD": "value",
"StringE":
{
"key": "value",
}
}
}The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!
-
Hi, I need to split a large string and split that splitted string again. I have the following string:
root
{
stringB =
{
"key": "value",
"key": "value",
"StringC":
{
"key": "value",
"key": "value",
"key": "value",
"key": "value",
}
"StringD": "value",
"StringE":
{
"key": "value",
}
}
}The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!
I believe you are trying to parse a JSON string. If yes then this link might be of some help to you!
-
Hi, I need to split a large string and split that splitted string again. I have the following string:
root
{
stringB =
{
"key": "value",
"key": "value",
"StringC":
{
"key": "value",
"key": "value",
"key": "value",
"key": "value",
}
"StringD": "value",
"StringE":
{
"key": "value",
}
}
}The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!
You can parse using JavaScriptSerializer.. This may help to you
string s = "{\"StringC\":"+
"{"+
"\"key1\": \"value\"," +
"\"key2\": \"value\"," +
"\"key3\": \"value\"," +
"\"key4\": \"value\"" +
"},"+
"\"StringD\": \"value\"," +
"\"StringE\":" +
"{"+
"\"key1\": \"value\"" +
"}"+
"}";JavaScriptSerializer f =new JavaScriptSerializer(); JsonData jsd = f.Deserialize<JsonData>(s);
public class JsonData
{
public Hashtable StringC { get; set; }
public string StringD {get; set;}
public Hashtable StringE { get; set; }
} -
Hi, I need to split a large string and split that splitted string again. I have the following string:
root
{
stringB =
{
"key": "value",
"key": "value",
"StringC":
{
"key": "value",
"key": "value",
"key": "value",
"key": "value",
}
"StringD": "value",
"StringE":
{
"key": "value",
}
}
}The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!
Just so you know, your sample string isn't very consistent. I would think there would have to be a comma after the closing braces. Beyond that, it seems to me you want to
Split
on the "{" character, and then the "," character on each of the first split result strings. I would probably write a method that accepted the delimiter character and the string to be split that returned an array of split items. Regex is not what you want to use - sometimes, you just gotta write the code.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
Hi, I need to split a large string and split that splitted string again. I have the following string:
root
{
stringB =
{
"key": "value",
"key": "value",
"StringC":
{
"key": "value",
"key": "value",
"key": "value",
"key": "value",
}
"StringD": "value",
"StringE":
{
"key": "value",
}
}
}The result I actually want is something like this: string[] AllStrings = StringB,StringC,StringD,StringE Is there a way to do this? I used Regex.Split option. Thanks in advance!
Try
(.*((\"([\d\w]+)\":)|(\s([\d\w]+) =)))*
The group no 4 contains keys like
key
orstringC
and group 6 contins keys likeStringB
EDIT: Of course you need to codestring expr = @"(.*((\"([\d\w]+)\":)|(\s([\d\w]+) =)))*";
var m = Regex.Match(expr);
var group4 = m.Groups[4];
var group6 = m.Group[6];
string[] allStrings = group4.Captures.Select(c=>c.Value).Union(group6.Select(c=>c.Value)).ToArray();captures now contains strings you need DISCLAIMER: 1. not tested. 2. use named groups if you want to.
Greetings - Jacek
modified on Wednesday, June 15, 2011 3:38 AM