T
T
TechNOIR2019-02-12 11:53:51
C++ / C#
TechNOIR, 2019-02-12 11:53:51

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

2 answer(s)
V
VoidVolker, 2019-02-12
@VoidVolker

try 
{
    output = File.ReadLines(tempOutputFile + ".txt").Single(l => Regex.IsMatch(l, "^.*[0-9][А-я].*$"));
} 
catch(Exception e)
{

}

S
shai_hulud, 2019-02-12
@shai_hulud

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 question

Ask a Question

731 491 924 answers to any question