Answer the question
In order to leave comments, you need to log in
C#. How to eliminate the error in this case?
Good afternoon.
When searching for a string in a file using a regular expression, if it does not find a result, it throws an error.
How can you eliminate the error? To continue silently if not found
output = File.ReadLines(tempOutputFile + ".txt").Single(l => Regex.IsMatch(l, "^.*[0-9][А-я].*$"));
Answer the question
In order to leave comments, you need to log in
try
{
output = File.ReadLines(tempOutputFile + ".txt").Single(l => Regex.IsMatch(l, "^.*[0-9][А-я].*$"));
}
catch(Exception e)
{
}
Use SingleOrDefault instead of Single. Then output will be null if nothing is found.
If there can be more than one match then replace with FirstOrDefault, LastOrDefault
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question