Jump to content
Sign in to follow this  
karl Jonson

TRegEx.IsMatch question

Recommended Posts

Hi,

This code not giving the result I'm expecting ?

TRegEx.IsMatch(anyStr, '[A-Za-z0-9]{1,5}')

Ab123 gives true

Ab123456 gives true

 

I expected Ab12345 would give false because input is longer than 5

Why ///Ab123456 is giving true ? I expected result to be false as /// don't match pattern ! 

TIA

 

Share this post


Link to post

You didn't specify that the pattern had to exhaust the entire string. Capture the match, look at the match, and see. Use a Web tool to test regex and see the matches. This will help you learn more quickly. 

  • Like 1

Share this post


Link to post

thanks to Stefan 

 

uses
  RegularExpressions;

procedure TForm1.Button1Click(Sender: TObject);
var
  RegEx: TRegEx;
begin
  RegEx := TRegEx.Create('(^[\w\d]{1,5}$)'); // RegEx := TRegEx.Create('(^[\w\d]{5,5}$)');
  //
  if RegEx.IsMatch('ab123') then
    ShowMessage('match found')
  else
    ShowMessage('not found');
end;

 

Edited by programmerdelphi2k

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×