Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello, I have a text file in the form
111.111.111.111 21/03/2017 21:30:14
111.111.111.111  21/03/2017 21:30:18
111.111.111.111 21/03/2017 21:30:25
111.111.111.111 21/03/2017 21:31:12
111.111.111.111 21/03/2017 21:31:40
111.111.111.111 21/03/2017 21:32:14
111.111.111.111   21/03/2017 21:32:18

 such as you can parse this file and get together the ip address and time instead


What I have tried:

I could parsit only on a separate
Posted
Updated 13-Aug-18 19:43pm
Comments
Patrice T 13-Aug-18 15:14pm    
Try to make sentences.
Tural_m 13-Aug-18 15:39pm    
I tried it like this, but so only IP parsit

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
List<string> ls = new List<string>();
using (StreamReader stream = new StreamReader(@"path"))
{
var str = stream.ReadToEnd();
foreach (Match item in Regex.Matches(str, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
{
ls.Add(item.Value);
}
}
foreach (var item in ls)
{
Console.WriteLine(item);
}


}
}
}
Patrice T 13-Aug-18 16:01pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

Start by working with each line, and use string.Split to break it into the three parts IP, date, and time:
string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 3)
    {
    string IP = parts[0];
    string date = parts[1];
    string time = parts[2];
    ...
    }

Or use a Regex to extract the IP and the DateTime:
public static Regex regex = new Regex(
      "^(?<IP>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\s+(?<Date"+
      ">\\d\\d/\\d\\d/\\d{4})\\s+(?<Time>\\d\\d:\\d\\d:\\d\\d)",
    RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );
...
    Match m = regex.Match(line);
    if (m.Success)
        {
        string IP = m.Groups["IP"].Value;
        string date = m.Groups["Date"].Value;
        string time = m.Groups["Time"].Value;
        //...
        }

If you are going to play with regular expressions, then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900