Reputation: 13
I have string which contains two sets of data, call it 1 and 2 respectively. In the data strings each are separated by symbol @
or &
like:
@data1&data2@data1&data2@....
I want to extract it to become an array such as x[] = all the data1
, y[] = data2
I tried the method strtok
but after processing of the string the method gave me an inverted string like this:
"abcd" becomes an array of
d
c
b
a
Which does not work for my situation because I need to split it into two arrays in the order in which it was found.
Upvotes: 0
Views: 177
Reputation: 31616
Regular expressions allow one to parse and extract based on patterns within the "reg-ex" language.
For example to create a pattern follow these steps.
@
and a literal character &
. So an initial pattern contains @ &
. ( )
. So now our pattern looks like this @()&()
.\w+
. Which says we are looking for a word type character (a-Z and 0-9) which can be represented as a \w
. Since we are looking for 1 or more characters, we add a +
to the \w
making it \w+
. Now our pattern looks like this @(\w+)&(\w+)
Match
instances.string data = "@data1&something1@data2&something2";
string pattern = @"@(\w+)&(\w+)";
// The C# literal character `@` must precede the string holding regex pattern.
var matches = Regex.Matches(data, pattern)
.OfType<Match>()
.Select(mt => new { X = mt.Groups[1].Value, Y = mt.Groups[2].Value })
.ToList();
// See what the data looks like in `matches`
var XArray = matches.Select(item => item.X).ToList();
// See what the data looks like in "XArray"
var YArray = matches.Select(item => item.Y).ToList();
// See what the data looks like in "YArray"
To Learn More See
Even though the documentation is for Visual Studio 2010, it still is ok and good to use.
Upvotes: 0
Reputation: 4895
Assuming your data format is unchanged
string input = "@data1&data2@data3&data4";
var keyValues = input.Split(new[] { '@'}, StringSplitOptions.RemoveEmptyEntries) // split to ["data1&data2", "data3&data4"]
.Select(i => i.Split('&')) // split to [ ["data1", "data2"], ["data3" , "data4"] ]
.Select(k => new KeyValuePair<string, string>(k[0], k[1]));
var x = keyValues.Select(s => s.Key); // x array
var y = keyValues.Select(s => s.Value); // y array
Upvotes: 0
Reputation: 373
I have no idea what exactly is stored in your data1
, data2
, etc, but it seems like you need to use Split()
method of your string.
string input = "@data1&data2@data1&data2@";
string[] splitInput = input.Split({'@', '&'});
Each element of splitInput
will contain one of your data blocks.
Upvotes: 1