I
I
iamdivine2020-04-14 15:50:08
Regular Expressions
iamdivine, 2020-04-14 15:50:08

Whole line in c# regex?

Good afternoon, There is a lot of text divided line by line,
i.e. in each line a certain number of characters - then a break.
It is necessary that when a word is found, all lines where the given word exists are written to a buffer or variable, and anywhere.

var s = File.ReadAllText(textPath.Text);

                    Regex regex = new Regex(@"text", RegexOptions.IgnoreCase | RegexOptions.Multiline);

                    MatchCollection matches = regex.Matches(s);
                    if (matches.Count > 0)
                    {
                        foreach (Match match in matches)
                        {

                            // Write the string array to a new file named "WriteLines.txt".
                            using (StreamWriter outputFile = new StreamWriter(System.IO.Path.Combine(@"C:\", "mq.txt")))
                            {
                                    outputFile.WriteLine(match);
                            }
                        }
                       
                    }

The code in the regular expression is not correct, I know

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Pokrovsky, 2020-04-14
@iamdivine

(^.*text.*$)
^ - start of string
$ - end
.* - any number of any characters

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question