Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Regular Expressions??


This question is answered.


Permlink Replies: 8 - Last Post: Nov 10, 2015 11:42 AM Last Post By: Ian Branch
Ian Branch

Posts: 442
Registered: 9/23/99
Regular Expressions??  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 9, 2015 9:38 PM
Hi Guys,
Delphi D10 Seattle.
First time having to play with Regex type stuff so my knowledge level is 0.

I have a string, say sTemp, that should only contain capitals and numerals from a user input, eg. 'A1B2' or 'AB3ZX', etc. If I read the documentation correctly I need something like...
'If Not Regex(sTemp, '[^A-Z0-9]') then do something'..
I see reference to IsMatch but unsure.

Can someone please assist with what Unit(s) I need to use and how to construct the test.

Regards & TIA,
Ian

Edited by: Ian Branch on Nov 10, 2015 6:02 PM
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Regular Expressions??  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 12:08 AM   in response to: Ian Branch in response to: Ian Branch
Ian Branch

Posts: 442
Registered: 9/23/99
Re: Regular Expressions??  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 12:43 AM   in response to: Robert Triest in response to: Robert Triest
Hi Robert,
Thanks for your response.
This is my code..
unit Unit1;
 
interface
 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.RegularExpressionsCore, Vcl.StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  TRegEx: TPerlRegEx;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  TRegEx := tperlregex.Create;
  if TRegEx.IsMatch('A1B1', '[A-Z0-9]') = True then ShowMessage('A match.')
    else
      ShowMessage('No match.');
end;
 
end.


Delphi doesn't like the 'IsMatch' - Undeclared identifier.
Clearly I have something wrong..
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Regular Expressions??  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 1:13 AM   in response to: Ian Branch in response to: Ian Branch
You don't have to create the TRegEx and you use the wrong class in the uses.

uses System.RegularExpressions;

 
procedure TForm1.Button1Click(Sender: TObject); 
begin   
  if TRegEx.IsMatch('A1B1', '[A-Z0-9]') = True then 
    ShowMessage('A match.')     
  else
    ShowMessage('No match.');
end;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
//TRegEx: TPerlRegEx;
public
{ Public declarations }
end;

var
Form1: TForm1;

//never declare your var of the object/class/form global here
//but put it in the protected, private or public part. (see above)
//anyway you don't need it now !
TRegEx: TPerlRegEx;

implementation
Ian Branch

Posts: 442
Registered: 9/23/99
Re: Regular Expressions??  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 1:27 AM   in response to: Robert Triest in response to: Robert Triest
Hi Robert,
I think we are closer.
 
 procedure TForm1.Button1Click(Sender: TObject); 
 begin   
   if TRegEx.IsMatch('A1b1', '[A-Z0-9]') = True then     <<<<<Should return false therefore 'No Match'.  Returns 'A Match'.
     ShowMessage('A match.')     
   else
     ShowMessage('No match.');
 end;
 


Regards,
Ian
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Regular Expressions??
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 1:53 AM   in response to: Ian Branch in response to: Ian Branch
if TRegEx.IsMatch('A1b1', '^[A-Z0-9]+$')
Ian Branch

Posts: 442
Registered: 9/23/99
Re: Regular Expressions??  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 11:42 AM   in response to: Robert Triest in response to: Robert Triest
Hi Rpbert,
Thank you for your assistance & resolution.
Much appreciated.
Regards,
Ian
Tom Brunberg

Posts: 329
Registered: 12/27/04
Re: Regular Expressions??
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 1:17 AM   in response to: Ian Branch in response to: Ian Branch
Ian Branch wrote:

Hi Robert,
Thanks for your response.
This is my code..
unit Unit1;
 
interface
 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.RegularExpressionsCore, Vcl.StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  TRegEx: TPerlRegEx;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  TRegEx := tperlregex.Create;
  if TRegEx.IsMatch('A1B1', '[A-Z0-9]') = True then ShowMessage('A match.')
    else
      ShowMessage('No match.');
end;
 
end.


Delphi doesn't like the 'IsMatch' - Undeclared identifier.
Clearly I have something wrong..

'IsMatch' is a class function declared as member of 'TRegEx' record declared in
'System.RegularExpressions'. Do not redeclare it as a TPerlRegex.

Just remove the TRegEx var declaration and the call to TPerlRegex.Create and TRegEx.IsMatch will
work.

--
Tom Brunberg
firstname.lastname@welho.com

Ian Branch

Posts: 442
Registered: 9/23/99
Re: Regular Expressions??  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 11:40 AM   in response to: Tom Brunberg in response to: Tom Brunberg
Hi Tom,
Thank you for your input. Appreciated.
Regards,
Ian
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02