简体   繁体   中英

Compare a text file with a pattern of text in c#?

I have compared a text file with a pattern like ".." It is writing the whole line in to the log like this...

insert into depdb..fin_quick_code_met

But i need to write this alone depdb or depdb..fin_quick_code_met

ie) i need the word alone

if (line.contains(".."))
dest.WriteLine("LineNo : " + counter.ToString() + " : " +"           "+ line.TrimStart());

How to write this word alone? Any suggestion?

Something like:

        string line = "insert into depdb..fin_quick_code_met";
        foreach(Match match in Regex.Matches(line, @"(\w*)\.\.")) {
            Console.WriteLine(match.Groups[1]);
        }

or for just a single (the first) match on the line:

        Match match = Regex.Match(line, @"(\w*)\.\.");
        if (match.Success)
        {
            Console.WriteLine(match.Groups[1]);
        }

To get the full depdb..fin_quick_code_met ; the same code, but with @"\\w*\\.\\.\\w* , looking at match.Value .

If your line contains only one .. entry, then this will work

  var line= "insert into depdb..fin_quick_code_met";           
  if (line.contains(".."))
   {           
        var splitted = line.Split(new[] { ".." }, StringSplitOptions.RemoveEmptyEntries);
        var firstPart = splitted.First().Split(' ').Last();
        var secondPart = splitted.Last();
        var composed = firstPart + ".." + secondPart;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM