UshaP
UshaP

Reputation: 1291

Regular expression parsing issue

I need to parse the below string, and want to use regular expression but can figure the correct way to do it.

Input Sample (token separator is ; and inside the token is any Char i.e. M/W/D )

1W4;2W35;4M35;13W108

Expected output

List<string> points = new List<string>() {"1W", "2W", "4M", "13W"};
List<int> intervals = new List<int>() {4, 35, 35, 108};

Thanks for any help.

Upvotes: 1

Views: 149

Answers (3)

Oleks
Oleks

Reputation: 32343

You could just split your string on tokens by using string.Split and then parse each token using regex:

var exp = new Regex(@"^(?<point>\d+[a-zA-Z])(?<interval>\d+)$");
var str = "1W4;2W35;4M35;13W108";
var tokens = str.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
foreach (var token in tokens)
{
    var match = exp.Match(token);
    if (match != null)
    {
        points.Add(match.Groups["point"].Value);
        intervals.Add(int.Parse(match.Groups["interval"].Value));
    }
}

Upvotes: 7

porges
porges

Reputation: 30590

This is a fairly liberal regex, in that it will tolerate differences in the input (e.g. it doesn't care about ;):

var points = new List<string>();
var intervals = new List<string>();

foreach (Match match in Regex.Matches(input, @"(\d+[MWD])(\d+)"))
{
    points.Add(match.Groups[1].Value);
    intervals.Add(match.Groups[2].Value);
}

Upvotes: 0

aweis
aweis

Reputation: 5616

a more restrictive regex could be:

var regEx = new Regex("^([0-9]+[MWD])([0-9]+)$");

With group 1 and 2 as youre output:

var regEx = new Regex("^([0-9]+[MWD])([0-9]+)$");
var str = "1W4;2W35;4M35;13W108";
var vals = str.Split(';');
foreach (var v in vals)
{
    var match = regEx.Match(v);
    Console.WriteLine(match.Groups[1].Value);
    Console.WriteLine(match.Groups[2].Value);
}

Upvotes: 1

Related Questions