Split line based on specific pattern using Regex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tariqondego
    New Member
    • Jun 2014
    • 2

    Split line based on specific pattern using Regex

    i need to split lines in text file based on a specific pattern.The pattern is " 01/12/12 00:39 AT400003".

    The source file looks like:

    AT400003 2012-12-01 00:36:03.000 abcdefghij 01/12/12 00:39 AT400003 abcdefjhij 01/12/12 01:39 AT400003 abcdefghij 01/12/12 00:39 AT400003 abcdefjhij

    I need the output file to look like:

    AT400003 2012-12-01 00:36:03.000 abcdefghij
    01/12/12 00:39 AT400003 abcdefjhij
    01/12/12 01:39 AT400003 abcdefghij
    01/12/12 00:39 AT400003 abcdefjhij

    But I get:

    AT400003 2012-12-01 00:36:03.000 abcdefghij
    abcdefjhij
    abcdefghij
    abcdefjhij

    Below is the code:

    string[] lines = System.Text.Reg ularExpressions .Regex.Split(ke ep, @"\s\s\s\d\d \/\d\d\/\d\d\s\d\d\:\d\ d\s\s\w\w\d\d\d \d\d\d");
    foreach (string line in lines)
    { sw.WriteLine(li ne); }
    Please advise how i can have the split expressions also included
  • tariqondego
    New Member
    • Jun 2014
    • 2

    #2
    I have solved this:added a positive look ahead to the expression.(?=. ..)
    string[] lines = System.Text.Reg ularExpressions .Regex.Split(ke ep, @"(?=\s\s\s\d\d \/\d\d\/\d\d\s\d\d\:\d\ d\s\s\w\w\d\d\d \d\d\d)");
    foreach (string line in lines)
    { sw.WriteLine(li ne); }

    Comment

    Working...