Don't do it all in one statement. Use the statements each on it's own line assigning to a variable and checking for validity.
Possible error sources with your code are that there is no match or the match can't be converted to an integer.
Another tip: To make the regex better readable use the
@
syntax to avoid multiple escaping:
@"(.+)\|"
You should use something like (untested):
int tSectionNo = 0;
Match match = Regex.Match(TempSectionTitleArray[icm], @"(.+)\|");
if (match.Success)
{
string sub = match.Groups[1].Value;
if (!Int32.TryParse(sub, out tSectionNo))
{
}
}
else
{
}
[EDIT]
There is another probable error source when the input string contains multiple
|
characters. Then the regex would match all the text before the last
|
(greedy matching) so that converting to
int
fails. To avoid that specify non-greedy (lazy) matching:
@"(.+?)\|"
[/EDIT]