Read text file document between 2 positions in c#

CVE 0 Reputation points
2023-07-27T12:56:43.6+00:00

I have multiple lines of text in a text document that are as follows;

    21)      3000.5  Rx         0011  8  0E 00 00 00 C3 3C D4 FF 

I need to snip each line like below;

0011 8 0E 00 00 00 C3 3C D4 FF

and past all the snippets into a textbox.

can anyone help

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. P a u l 10,496 Reputation points
    2023-07-27T17:45:58.5033333+00:00

    You could use regex provided the format of each line is the same as the above:

    using System.Text.RegularExpressions;
    
    var input = @"
        21)      3000.5  Rx         0011  8  0E 00 00 00 C3 3C D4 FF 
        21)      3000.5  Rx         0011  8  0E 00 00 00 C3 3C D4 FE 
        21)      3000.5  Rx         0011  8  0E 00 00 00 C3 3C D4 FD 
    ";
    
    var results = new Regex(@"^\s*\d+\)\s+[\d.]+\s+Rx\s+(.+)$", RegexOptions.Multiline).Matches(input)
    	.Select(m => m.Groups[1].Value.Trim().Replace("  ", " "))
    	.ToArray();
    
    foreach (var result in results) {
    	Console.WriteLine(result);
    }
    

    Which produces:

    0011 8 0E 00 00 00 C3 3C D4 FF
    0011 8 0E 00 00 00 C3 3C D4 FE
    0011 8 0E 00 00 00 C3 3C D4 FD
    
    0 comments No comments