TomiTeruz
TomiTeruz

Reputation: 13

Create Regex Pattern for String using C#

I have string pattern like this:

#c1 12,34,222x8. 45,989,100x10. 767x55. #c1

I want to change these patterns into this:

c1,12,8
c1,34,8
c1,222,8
c1,45,10
c1,989,10
c1,100,10
c1,767,55

My code in C#:

private void btnProses_Click(object sender, EventArgs e)
{
    String ps = txtpesan.Text;
    Regex rx = new Regex("((?:\d+,)*(?:\d+))x(\d+)");
    Match mc = rx.Match(ps);
    while (mc.Success)
    {
        txtpesan.Text = rx.ToString();
    }
}

I've been using split and replace but to no avail. After I tried to solve this problem, I see many people using regex, I tried to use regex but I do not get the logic of making a pattern regex.

What should I use to solve this problem?

Upvotes: 1

Views: 3861

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

And here comes a somewhat ugly regex based solution:

var q = "#c1 12,34,222x8. 45,989,100x10. 767x55. #c1";
var results = Regex.Matches(q, @"(?:(?:,?\b(\d+))(?:x(\d+))?)+");
var caps = results.Cast<Match>()
     .Select(m => m.Groups[1].Captures.Cast<Capture>().Select(cap => cap.Value));
var trailings = results.Cast<Match>().Select(m => m.Groups[2].Value).ToList();
var c1 = q.Split(' ')[0].Substring(1);
var cnt = 0;
foreach (var grp in caps)
{
     foreach (var item in grp)
     {
         Console.WriteLine("{0},{1},{2}", c1, item, trailings[cnt]);
     }
     cnt++;
}

The regex demo can be seen here. The pattern matches blocks of comma-separated digits while capturing the digits into Group 1, and captures the digits after x into Group 2. Could not get rid of the cnt counter, sorry.

Upvotes: 0

pwas
pwas

Reputation: 3373

sometimes regex is not good approach - old school way wins. Assuming valid input:

var tokens = txtpesan.Text.Split(' '); //or use split by regex's whitechar
var prefix = tokens[0].Trim('#');

var result = new StringBuilder();

//skip first and last token
foreach (var token in tokens.Skip(1).Reverse().Skip(1).Reverse())
{
    var xIndex = token.IndexOf("x");
    var numbers = token.Substring(0, xIndex).Split(',');
    var lastNumber = token.Substring(xIndex + 1).Trim('.');

    foreach (var num in numbers)
    {
        result.AppendLine(string.Format("{0},{1},{2}", prefix, num, lastNumber));
    }
}

var viola = result.ToString();
Console.WriteLine(viola);

Upvotes: 2

Related Questions