CoderXX
CoderXX

Reputation: 81

Parsing a string in C# using Regex

I have string of a particular type (A, B) C | D 0, 7 | E 0, 6 | F 0, 6 where A, B, C, D, E and F are known but the numbers has to be extracted.

Is there a way by which this can be done using Regex or something else in C#?

Upvotes: 0

Views: 89

Answers (1)

Grant Winney
Grant Winney

Reputation: 66449

Assuming that's always the format of the string you'd be parsing, you could just use a simple String.Split:

var elements = yourString.Split(new[] {'(', ')', '|', ',', ' '},
    StringSplitOptions.RemoveEmptyEntries);

Then just use elements[0] to get the value represented by A, etc. and cast it back to an integer or whatever you need to do with it.

Upvotes: 2

Related Questions