TMatchCollectionCount (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of TMatchCollection and TGroupCollection. This example assumes that you have placed a TButton, a TEdit and a TMemo on a form.

Code

TForm1 *Form1;
TMatchCollection mycoll;
TMatchCollectionEnumerator *myenum;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// Creates and lists the match collection, the matches in that
// collection and the groups in those matches.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  const String bigString = "Look for a the strings in this strang of strungs.";
  const String littlestring = "(str)([iau]ng)";
 
  TRegEx regex(littlestring, TRegExOptions() << TRegExOption::roNotEmpty);
  mycoll = regex.Matches(bigString);
  Edit1->Text = "Count: " + IntToStr(mycoll.Count);
  Memo1->Lines->Add("First Collection: ");
  for (int i = 0; i < mycoll.Count; i++)
  {
    Memo1->Lines->Add("Match #" + IntToStr(i) + ": " + mycoll.Item[i].Value);
    Memo1->Lines->Add("Group: " + IntToStr(i));
    TGroupCollection mygrps = mycoll.Item[i].Groups;
    for (int j = 0; j < mygrps.Count; j++)
      Memo1->Lines->Add("Value: " + mygrps.Item[j].Value);
  };
}
//---------------------------------------------------------------------------
// Moves the enumerator to the next member of the collection.
// Create the match collection and set the enumerator first.
// MoveNext only moves to the last, does not wrap to the first.
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  myenum->MoveNext();
}
//---------------------------------------------------------------------------
// Sets the enumerator in the current match collection.
void __fastcall TForm1::Button3Click(TObject *Sender)
{
  myenum = mycoll.GetEnumerator();
}
//---------------------------------------------------------------------------
// Create a different match collection.
// Note that MoveNext and Current work on the old match collection
// until the enumerator is set.
void __fastcall TForm1::Button4Click(TObject *Sender)
{
  const String bigString = "Look for a the strings.";
  const String littlestring = "str[iau]ng";
 
  TRegEx regex(littlestring, TRegExOptions() << TRegExOption::roNotEmpty);
  mycoll = regex.Matches(bigString);
  Edit1->Text = "Count: " + IntToStr(mycoll.Count);
  for (int i = 0; i < mycoll.Count; i++)
    Memo1->Lines->Add("Second collection: " + mycoll.Item[i].Value);
}
//---------------------------------------------------------------------------
// Displays the current match of the collection.
// Current does not work unit the match collection is created,
// the enumeration is set and MoveNext sets the index to the first member.
void __fastcall TForm1::Button5Click(TObject *Sender)
{
  Memo1->Lines->Add("Enum current: " + myenum->Current.Value);
}
//---------------------------------------------------------------------------

Uses