1

I have a string that looks like this:

123.45.67.890-1292 connected to EDS via 10.98.765.432-4300.

I need to split it like so:

   "123.45.67.890-1292 connected to EDS via 10.98.765.432-4300."
    -----+------- --+-              -+-     -----+------- --+-
         |          |                |           |          |
  ClientIP          |      ServiceName      HostIP          |
                    |                                       |
        ClientSession                             HostSession

I'm converting the code from vbscript that has a lot of complex InStr methods. Was wondering if there was a way to do this using a regEx.

6
  • 9
    I think writing that nice ascii graphics took longer than it would have taken you to learn enough about regular expressions to make this happen. :) Commented Oct 3, 2011 at 21:10
  • 2
    are "connected to" and "via" present in every case? Commented Oct 3, 2011 at 21:11
  • answer: yes there is. OT: @IAbstractDownvoteFactory - awesome screen name Commented Oct 3, 2011 at 21:11
  • Why can't you use split, and why is RegEx the answer? If you're doing this at large scale, consider codinghorror.com/blog/2006/01/regex-performance.html Commented Oct 3, 2011 at 21:13
  • 3
    Why can't you use split? Seems like splitting on space and getting them from their indexes would be easy. Commented Oct 3, 2011 at 21:13

5 Answers 5

8

(\d{,3}\.\d{,3}\.\d{,3}\.\d{,3})-(\d+) connected to ([A-Z]+) via (\d{,3}\.\d{,3}\.\d{,3}\.\d{,3})-(\d+)\.

Sign up to request clarification or add additional context in comments.

Comments

1

Why can't you use split? Using regular expression for single task is inappropriate:

([^\-]+)\-(\S+)\s+connected\s+to\s+(\S+)\s+via\s+([^\-]+)\-(\S+)\.

C# code implementation (regular expression):

static void Main(string[] args)
{
    String input = "123.45.67.890-1292 connected to EDS via 10.98.765.432-4300.";
    String pattern = @"([^\-]+)\-(\S+)\s+connected\s+to\s+(\S+)\s+via\s+([^\-]+)\-(\S+)\.";

    Match match = Regex.Match(input, pattern);

    if (match.Success)
    {
        foreach (var group in match.Groups)
        {
            Console.WriteLine(group);
        }
    }

    Console.ReadKey();
}

C# code implementation (splitting):

public class DTO
{
    public string ClientIP { get; set; }
    public string ClientSession { get; set; }
    public string ServiceName { get; set; }
    public string HostIP { get; set; }
    public string HostSession { get; set; }
}

static void Main(string[] args)
{
    String input = "123.45.67.890-1292 connected to EDS via 10.98.765.432-4300.";
    String[] splits = input.Split(new char[] { ' ' });

    DTO obj = new DTO();

    for (int i = 0; i < splits.Length; ++i) 
    {
        switch (i) 
        {
            // connected
            case 1:
            // to
            case 2:
            // via 
            case 4:
                {
                    break;
                }

            // 123.45.67.890-1292
            case 0:
                {

                    obj.ClientIP = splits[i].Split(new char[] { '-' })[0];
                    obj.ClientSession = splits[i].Split(new char[] { '-' })[1];

                    break;
                }

            // EDS
            case 3:
                {
                    obj.ServiceName = splits[i];
                    break;
                }

            // 10.98.765.432-4300.
            case 5:
                {
                    obj.HostIP = splits[i].Split(new char[] { '-' })[0];
                    obj.HostSession = splits[i].Split(new char[] { '-' })[1];

                    break;
                }

        }

    }

    Console.ReadKey();  
}

1 Comment

thanks for the full code. That was a better solution then I had.
1

(?<ClientIP>\d+\.\d+\.\d+\.\d+)-(?<ClientSession>\d+) connected to (?<ServiceName>.*?) via (?<HostIP>\d+\.\d+\.\d+\.\d+)-(?<HostSession>\d+)\.

Comments

1

Here's a RegExp to match/capture that:

([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)-([0-9]+) connected to ([a-zA-Z]+) via ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)-([0-9]+)

implementation:

        string pat = @"([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)-([0-9]+) connected to ([a-zA-Z]+) via ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)-([0-9]+)";
        Regex r = new Regex(pat, RegexOptions.IgnoreCase);
        Match match = r.Match("123.45.67.890-1292 connected to EDS via 10.98.765.432-4300.");

        foreach (var str in match.Groups)
            Console.WriteLine(str);
        Console.ReadKey();

1 Comment

Thanks. Decided to go the split route. It's just slightly cleaner for my needs.
1

Since I don't see why you rule out String.Split() :

var parts = test.Split(new string[] {" connected to ", " via "},
      StringSplitOptions.None);

gives you

123.45.67.890-1292
EDS
10.98.765.432-4300

breaking of the -#### session parts would take 1 extra step, also possible with Split().

Or maybe easier:

 var parts = test.Split(' ', '-');

and use parts 0, 1, 4, 6, 7

1 Comment

This is what I was thinking but don't you lose the extra benefit that RegEx also validates that its an IP address-session

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.