• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

splitt string with regex with c#

Hello everybody,

i have the the following two strings:

D 1069 Dresden - Neustadt
01069 Dresden - Neustadt

I want to splitt the string with c# and the result should be in both cases:

1069
Dresden - Neustadt

How could i do this with regular expression?

thank you very much
Kind regards
bounty
0
bounty457
Asked:
bounty457
  • 3
1 Solution
 
Terry WoodsIT GuruCommented:
Try doing a match with the following pattern (multiline mode turned on):
([1-9]\d*) (.*$)

Open in new window

It will capture the text for each line into separate groups.

I'm not a C# programmer, but the myregextester.com website generated the following code. Hopefully that helps.
using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "source string to match with pattern";
          Regex re = new Regex(@"([1-9]\d*) (.*$)",RegexOptions.Multiline);
          MatchCollection mc = re.Matches(sourcestring);
          int mIdx=0;
          foreach (Match m in mc)
           {
            for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
              {
                Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
              }
            mIdx++;
          }
        }
    }
}

Open in new window

0
 
bounty457Author Commented:
Great, it works Thank you very much.
Kind regards
bounty
0
 
bounty457Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for bounty457's comment #a40287489

for the following reason:

Thank you for the fast solution.
0
 
käµfm³d 👽Commented:
I'm thinking you mean to accept Terry's comment as the solution?
0
 
bounty457Author Commented:
Hello eenookami,

yes i accept Terry's comment as solution, I have clicked on "accept as solution" but i can't select the points. There was no link or button available.

Thank you for accepting as solution with 500 points.
kind regards
bounty
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now