RTL.RegExpressionVCL Sample

From RAD Studio Code Examples
Jump to: navigation, search

RegExpDemo is a sample that demonstrates how to use regular expressions in an application.

Location

You can find the RegExpDemo sample project at:

  • Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to one of the following:
    • Object Pascal\RTL\RegExpression\vcl
    • CPP\RTL\RegExpression\vcl
  • Subversion Repository
    • You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

Description

This application allows the user to test the validity of a string with regards to a regular expression.

The form contains a list box, a memo, an edit box, and a button. From the list box the user can choose to validate an e-mail address, an IP address, or a date in either of two formats. When one of the options is selected from the TListBox, the regular expression that is used for validation is displayed in the TMemo component. Next, the user can enter a string in the TEdit and click Evaluate to see whether the input is valid according to the regular expression.

How to Use the Sample

  1. Navigate to one of the locations given above and open:
    • Delphi: RegExpDemo.dproj.
    • C++: CppRegExpDemo.cbproj.
  2. Press F9 or choose Run > Run.

To interact with the sample:

  • Select the type of expression from the lbRegExp list box.
  • Type the input string you want to evaluate in the EditText edit and click Evaluate.
  • You can edit the regular expression in the MemoRegEx memo.

Files

File in Delphi File in C++ Contains

RegExpDemo.dproj

CppRegExpDemo.cbproj

The project itself.

Main.dfm

Main.dfm

The main form where the components are located.

Main.pas

Main.h, Main.cpp

Implementation of the sample.

Classes

TForm1, the class for the main form, handles the display of visual components and the validation of strings by using regular expressions.

Implementation

  • The TListBox.OnClick event is used to know when an item is selected on the list box; then the corresponding regular expression is added to the TMemo.
  • The input string is evaluated with the TRegEx.IsMatch method.
  • A message shows whether the input corresponds with a regular expression or not.

Uses

See Also