Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have the following line of code:

int tSectionNo = Convert.ToInt32(Regex.Match(TempSectionTitleArray[icm], "(.+)\\|").Groups[1].Value);


and I am getting the following error:

Input string was not in a correct format


TempSectionTitleArray
is a String array initialized to null by default.

Please help.

Regards
Aman Chaurasia

What I have tried:

int tSectionNo = Convert.ToInt32(Regex.Match(TempSectionTitleArray[icm], "(.+)\\|").Groups[1].Value);
Posted
Updated 25-May-18 0:34am
v2

1 solution

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:
C#
@"(.+)\|"

You should use something like (untested):
C#
int tSectionNo = 0;
Match match = Regex.Match(TempSectionTitleArray[icm], @"(.+)\|");
if (match.Success)
{
    // Assign to a variable so that it can be inspected and/or 
    //  added to an error message
    string sub = match.Groups[1].Value;
    if (!Int32.TryParse(sub, out tSectionNo))
    {
        // Not an integer
    }
}
else
{
    // No match
}

[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:
C#
@"(.+?)\|"
[/EDIT]
 
Share this answer
 
v2
Comments
Primo Chalice 25-May-18 7:06am    
else
{
TempSectionStringArray = TempSectionString.Split(' ');
TempSectionTitleArray = TempSectionTitleString.Split(' ');
bool SectionsWritten = false;
for (var icm = 0; icm < TempSectionTitleArray.Length; icm++)
{
if (!string.IsNullOrEmpty(TempSectionTitleArray[icm]))
{
int tSectionNo = Convert.ToInt32(Regex.Match(TempSectionTitleArray[icm], @"(.+)\\|").Groups[1].Value);
string tSectionTitle = Convert.ToString(Regex.Match(TempSectionTitleArray[icm], @"\\|(.+)").Groups[1].Value).Trim(' ');
string TStrSectionHead = "";
TStrSectionHead = StrSectionHead;
TStrSectionHead = Regex.Replace(TStrSectionHead, "<div class=\"sectionName\">" + TitleTextBox.Text + "", "<div class=\"sectionName\">" + tSectionTitle + "", RegexOptions.Multiline);

for (var ic = 0; ic < TempSectionStringArray.Length; ic++)
{
if (!string.IsNullOrEmpty(TempSectionStringArray[ic]))
{
string ArticleSectionTitleNo = Convert.ToInt32(Regex.Match(TempSectionStringArray[ic], "(.+)\\|").Groups[1].Value).ToString();
if (ArticleSectionTitleNo == tSectionNo.ToString())
{
string ArticleFileStr = Regex.Match(TempSectionStringArray[ic], "\\|(.+)").Groups[1].Value;
foreach (var ArticleFile in dinfo)
{
string tArtName = null;
tArtName = ArticleFile.Name;
if (tArtName.Contains(ArticleFileStr))
{
try
{
string tstrFileContent = "";
tstrFileContent = Common.ReadFile(ArticleFile.FullName);
StrSectionBody = StrSectionBody + "<div class=\"article-spacer\"/>" + Environment.NewLine;
StrSectionBody = StrSectionBody + "<div id=\"preview_" + ArticleBodyCount + "\" class=\"sectionArticleBlockOdd\">" + Environment.NewLine;
StrSectionBody = StrSectionBody + "<div class=\"sectionArticleTextBlock\">" + Environment.NewLine;
StrSectionBody = StrSectionBody + "<div class=\"sectionArticleTitle\">" + Environment.NewLine;
StrSectionBody = StrSectionBody + "<a class=\"sectionArticleLink\" href=\"" + ArticleFile.Name + "\">" + Environment.NewLine;
string ArticleHeadTitle = "";
if (Regex.IsMatch(tstrFileContent, "<h2 id=.+>(.+?)", RegexOptions.Multiline))
{
ArticleHeadTitle = Regex.Match(tstrFileContent, "<h2 id=.+>(.+?)"
Jochen Arndt 25-May-18 7:18am    
???

What do you want to tell me by posting such a bunch of unformatted code?

You have not followed my suggestion to check the intermediate results. All you have done is using the @ prefix for the regex string but failed because the double backslash is still there.

Finally, if you want to get the parts before and after the '|' character, why not use a single regex with two matches @"(.+?)\|(.+)"?

I suggest to read about regular expressions and print out the intermediate results so that you see what is happening and where you get wrong / unexpected results.
Primo Chalice 26-May-18 3:58am    
I tried the expression you wrote in [EDIT], but the error remains. Actually I am showing you the Visual Basic code of the same line. In Visual Basic, it runs perfectly without any bugs or errors, with the same Regular Expression. The code is below:

Dim tSectionNo As Integer = CInt(Regex.Match(TempSectionTitleArray(icm), "(.+)\|").Groups(1).Value)
Jochen Arndt 26-May-18 4:17am    
Again:
Split the statement into it parts. Then you can use the debugger or print the parts out to see where the error occurs for what kind of input.

Also is casting using CInt not the same as using a conversion function because it is less restrictive.
Primo Chalice 26-May-18 4:56am    
I put a MessageBox to see what data is stored. It is storing no more than or equal to 2 values in the array, thus the error. If it is 1 value, then it is working fine.

Also, what shall I use as the alternative for CInt in C#?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900