How can I remove a line from a string that includes a certain value C#?

Charlie Gale 21 Reputation points
2022-07-02T23:53:17.487+00:00

I am creating an analysis program which displays data very rapidly. Alot of this data is just noise and therefore I want to make a filtering function.

Data is read and displayed using SerialPort.ReadExisting(); and therefore is returned in "Clumps". This means just searching for the ID and removed all of the string will not work.

Data is sent through serial port and is displayed as such.

ID: 201, Data: 0 0 40 0 0 0 0 80
ID: 201, Data: 0 0 40 0 0 0 0 80
ID: 20F, Data: FF FF 27 10 40 0 FF 77
ID: 190, Data: 0 53 0 0 3C 1 0 3C
ID: 20F, Data: FF FF 27 10 40 0 FF 7D
ID: 280, Data: 0 9
ID: 201, Data: 0 0 40 0 0 0 0 80
ID: 231, Data: 1 6F 64 0 0 0 80
ID: 205, Data: 2 95 1 F5 3 F4 1 F5
ID: 201, Data: 0 0 40 0 0 0 0 80
ID: 4B0, Data: 27 10 27 10 27 10 27 10
ID: 20F, Data: FF FF 27 10 40 0 FF 98
ID: 20F, Data: FF FF 27 10 40 0 FF 9B
ID: 190, Data: 0 54 0 0 3C 1 0 3C
ID: 4B0, Data: 27 10 27 10 27 10 27 10
ID: 280, Data: 0 9
ID: 20F, Data: FF FF 27 10 40 0 FF AA
ID: 433, Data: 80 4 50 1 0 A0 0 0
ID: 20F, Data: FF FF 27 10 40 0 FF AF
ID: 280, Data: 0 9

The data is read from an arduino.

I want to be able to submit an ID and the whole line to be removed.

I have tried using;
public string filterIDs(string dataIn) { foreach (var filterID in filteredIDs) dataIn = Regex.Replace(dataIn, @$"^ID: {Regex.Escape(filterID)},.*\n", "", RegexOptions.Multiline); return dataIn; } which did not have any results.

and

foreach (var filterID in filteredIDs)
{

dataIn = Regex.Replace(dataIn,filterID, "", RegexOptions.Multiline);

}
return dataIn;
which only removed the ID numbers.

I have used other Regex values but they did not work either, but this may just be from my lack of experience with the topic.

filteredIDs values are kept as "ID: NUM".

I am new to using Regex so any help would be useful.

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-07-04T02:35:38.28+00:00

    @Charlie Gale , Welcome to Microsoft Q&A, have you considered another method without using Regex? It may be more simpler.

    Please refer to the following code to use linq to remove a line which contains the certain value.

     static void Main(string[] args)  
            {  
                var result = filterIDs("201");  
            }  
            public static string filterIDs(string dataIn)   
            {  
                var filteredIDs = File.ReadAllLines("1.txt");  
                var notincluded = filteredIDs.Where(x => !x.Contains(dataIn));  
                string result=string.Join("\r", notincluded.ToArray());  
      
                return result;  
      
            }  
    

    Note: Here I used txt file as the data source and you could make some changes based on your situation.

    Tested result:

    217203-image.png

    Hope this could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Charlie Gale 21 Reputation points
    2022-07-05T14:44:25.457+00:00

    I have made a small function of:

    			foreach (var filterID in filteredIDs)  
    			{  
      
    				int start = dataIn.IndexOf(filterID);  
    				int end = (dataIn.IndexOf("\n")+1);  
    				string remove = dataIn.Substring(start, end - start);  
    				dataIn = Regex.Replace(dataIn, remove, "", RegexOptions.Multiline);  
      
    			}  
    			return dataIn;  
    

    which works fine with my situation.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.