This is my regex originally from VB.

using System.Text.RegularExpressions;
#region "Regex"

        public object preg_match(System.Object sRegex, System.Object sHaystack)
   {
	// Declare some varis
	MatchCollection Matches = default(MatchCollection);
	// My Regex to match some text. It is actually a whole <div> block.
	Regex TheRegex = new Regex("sRegex");
	// Get the Source code from the page
	string t = sHaystack.ToString();
	// Perform the match
	Matches = TheRegex.Matches(t);
	// Match for 1
	Match Match = default(Match);
	Match = TheRegex.Match(t);
	// Search for all the words in a string
	Match AllMatches = default(Match);
	int iCount = 0;
	int iResult = 0;
	int iMaxResults = 0;
	// Loop through the matches to store in an array
	// From using PHP my whole existance, I like to have the
	// Matches in an array
	if (!Match.Success) {
		string[,] aRes = new string[1, 1];
		aRes[0, 0] = "";
		return aRes;
	}
	string[,] aResults = new string[Matches.Item(0).Groups.Count + 1, Matches.Count + 1];
	foreach (AllMatches in Matches) {
		iCount = 0;
		// We have sub results we want to store.

		foreach (Group blah in AllMatches.Groups) {
			aResults[iCount, iResult] = blah.Value;
			iCount += 1;
		}

		if (iMaxResults < iResult) {
			iMaxResults = iResult;
		}
		iResult += 1;
	}

	aResults[iCount, 0] = iMaxResults.ToString() + 1;

	return aResults;
}
        #endregion

The Part with errors

string[,] aResults = new string[Matches.Item(0).Groups.Count + 1, Matches.Count + 1];
	foreach (AllMatches in Matches)

Dani AI

Generated

The compile error shown by ("Type and identifier are both required in a foreach statement") is a classic C# syntax/translation issue plus a few leftover VB habits. correctly pointed out the missing typed iterator — that only fixes the immediate compiler message. Several subtle bugs remain that will cause logic and runtime problems.

Key fixes to apply (in this order):

  • Make the foreach declare the element type and use the loop variable inside the inner loop instead of the leftover variable from the VB translation. The inner loop must iterate the Groups collection of the current Match, not a different, uninitialized variable.
  • Stop naming local variables the same as CLR types (for example a variable named Match). Rename such locals to avoid shadowing and confusion.
  • Use the actual pattern string passed into the method instead of the literal "sRegex" when constructing the Regex object; ensure proper escaping or use a verbatim string and the right RegexOptions (Singleline/Multiline) if the pattern spans lines.
  • Declare all formerly-implicit VB identifiers with explicit C# types: sTmp as string, iPg as int, tmpPages either as an array (access with square brackets) or a method (call with parentheses), and web as a method that returns a string. VB-style parentheses for array access will not compile in C#.
  • Prefer a strongly typed return value instead of object (return MatchCollection, a List<string[]>, or a clearly-dimensioned string[,]) so callers and compiler can catch errors earlier.

Additional notes and best practices:

  • Parsing HTML with regular expressions is brittle; consider an HTML parser such as HtmlAgilityPack for robust extraction (HtmlAgilityPack).
  • Reference docs for exact syntax and options: Regex class and C# foreach syntax (foreach in C#).
  • Watch logical operators and null checks (use || for logical OR and string.IsNullOrEmpty appropriately) and validate array dimensions before indexing to avoid runtime exceptions.

Applying these changes will remove the compiler errors shown in the thread and make the regex extraction logic safer and easier to maintain.

Recommended Answers

All 15 Replies

What errors are you getting?

Try using this

string[,] aResults = new string[Matches[0].Groups.Count + 1, Matches.Count + 1];

Didn't test it, but it could point you to the right direction
Regards

It fixed one error...

No.1 Error for the in

Error	6	Type and identifier are both required in a foreach statement	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\iUltimate\iUltimate\Form3.cs	76	29	iUltimate

No.2 Error for the All Matches, what should of been definded already.

Error	7	The type or namespace name 'AllMatches' could not be found (are you missing a using directive or an assembly reference?)	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\iUltimate\iUltimate\Form3.cs	76	18	iUltimate

Dont worry about No2 Its fixed.

Should you be supplying a type for allmatches?

Matches AllMatches
Regards

No, Please concentrate on Error No.1

Post what you have now. Maybe we could locate the problem.

Here you go

Error	2	Type and identifier are both required in a foreach statement	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\iUltimate\iUltimate\Form3.cs	74	29	iUltimate

No I mean the whole code (recently updated).

I think there's a problem with how you did your foreach.

Regards

Oh ok

#region "Regex"

        public object preg_match(System.Object sRegex, System.Object sHaystack)
   {
	// Declare some varis
	MatchCollection Matches = default(MatchCollection);
	// My Regex to match some text. It is actually a whole <div> block.
	Regex TheRegex = new Regex("sRegex");
	// Get the Source code from the page
	string t = sHaystack.ToString();
	// Perform the match
	Matches = TheRegex.Matches(t);
	// Match for 1
	Match Match = default(Match);
	Match = TheRegex.Match(t);
	// Search for all the words in a string
	Match AllMatches = default(Match);
	int iCount = 0;
	int iResult = 0;
	int iMaxResults = 0;
	// Loop through the matches to store in an array
	// From using PHP my whole existance, I like to have the
	// Matches in an array
	if (!Match.Success) {
		string[,] aRes = new string[1, 1];
		aRes[0, 0] = "";
		return aRes;
	}
        string[,] aResults = new string[Matches[0].Groups.Count + 1, Matches.Count + 1];	
        foreach (AllMatches in Matches) {
		iCount = 0;
		// We have sub results we want to store.

		foreach (Group blah in AllMatches.Groups) {
			aResults[iCount, iResult] = blah.Value;
			iCount += 1;
		}

		if (iMaxResults < iResult) {
			iMaxResults = iResult;
		}
		iResult += 1;
	}

	aResults[iCount, 0] = iMaxResults.ToString() + 1;

	return aResults;
}
        #endregion

Like i said on my earlier post,shouldn't you be supplying a type for allmatches?

foreach (Match match in Matches)

Like i said on my earlier post,shouldn't you be supplying a type for allmatches?

foreach (Match match in Matches)

Ok, thanks. That takes care of that problem.... Now I need to deal with using that regex. Here is a code that uses it

sTmp = web("" + tmpPages(1, iPg));
            tmp = preg_match("<tr><td>([^<]*?)<script\\ type=\"text/javascript\">document\\.write\\(\":\"([a-z\\+]*?)\\)", sTmp);

            if (string.IsNullOrEmpty(tmp(0, 0)) | tmp(0, 0) == null)
            {

            }

Every tmp has a error, sTmp, web. iPg, and tmpPages all have errors. They didn't in VB. Is there a system I am suppose to be using?

Do you mind showing us those errors? and providing us the other descriptions like what type are you using.

Regards

PS:

Best if you show us the whole code.

ok here lol

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Net.NetworkInformation;
using System.IO;
using System.Text.RegularExpressions;
using DevComponents.DotNetBar;


namespace iUltimate
{
    public partial class Form3 :  Office2007RibbonForm
    {
        int proxiesIterator = 0;
        public Form3()
        {
            InitializeComponent();

        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }
        
        #region "Regex"

        public object preg_match(System.Object sRegex, System.Object sHaystack)
   {
	// Declare some varis
	MatchCollection Matches = default(MatchCollection);
	// My Regex to match some text. It is actually a whole <div> block.
	Regex TheRegex = new Regex("sRegex");
	// Get the Source code from the page
	string t = sHaystack.ToString();
	// Perform the match
	Matches = TheRegex.Matches(t);
	// Match for 1
	Match Match = default(Match);
	Match = TheRegex.Match(t);
	// Search for all the words in a string
	Match AllMatches = default(Match);
	int iCount = 0;
	int iResult = 0;
	int iMaxResults = 0;
	// Loop through the matches to store in an array
	// From using PHP my whole existance, I like to have the
	// Matches in an array
	if (!Match.Success) {
		string[,] aRes = new string[1, 1];
		aRes[0, 0] = "";
		return aRes;
	}
        string[,] aResults = new string[Matches[0].Groups.Count + 1, Matches.Count + 1];
        foreach (Match match in Matches)
        {
		iCount = 0;
		// We have sub results we want to store.

		foreach (Group blah in AllMatches.Groups) {
			aResults[iCount, iResult] = blah.Value;
			iCount += 1;
		}

		if (iMaxResults < iResult) {
			iMaxResults = iResult;
		}
		iResult += 1;
	}

	aResults[iCount, 0] = iMaxResults.ToString() + 1;

	return aResults;
}
        #endregion

       
        


        private void buttonX1_Click(object sender, EventArgs e)
        {
            sTmp = web("" + tmpPages(1, iPg));
            tmp = preg_match("<tr><td>([^<]*?)<script\\ type=\"text/javascript\">document\\.write\\(\":\"([a-z\\+]*?)\\)", sTmp);

            if (string.IsNullOrEmpty(tmp(0, 0)) | tmp(0, 0) == null)
            {

            }
        }

        
    }
}

Anything Else?

Also for each proxy it gets I need to add it to a list box

Where did you declared this tmpPages, sTpm, iPg? :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.