Jump to content
Sign in to follow this  
Chris1701

Using Regex for file name matching?

Recommended Posts

I'm trying to figure out how to use regular expressions to test file names for matching results and the test program that I have is failing rather badly. It's basically a form with some labels / TEdits / a listbox and a button, so enter the pattern in the pattern tedit and enter the text in the text tedit and click the test button to see if it matches:


procedure TMainForm.TestClick(Sender: TObject);

begin
  If TRegEx.IsMatch( edText.Text, edPattern.Text ) Then
    lbData.Insert( 0, 'True' )
   Else
    lbData.Insert( 0, 'False' );
end;

For example if I enter "*.pas" in the edPattern edit and "myfiletest.pas" in edPattern and click Test the program throws an error "Project RegExpressText.exe raised exception class ERegularExpressionError with message 'Error in regular expression at offset 0: nothing to repeat'." So none of the things I see to try such as pattern "*mydata*.*" and text "thisismydata.csv" generate the same error however the preloaded test data Pattern "Er*" and text "Er S01 D01" does not cause and error and comes back True. What am I doing wrong here?

 

Thanks!

Share this post


Link to post

A short answer is that *.pas is not a regular expression.  In this case:  *   The asterisk indicates zero or more occurrences of the preceding element. For example, ab*c matches "ac", "abc", "abbc", "abbbc", and so on.

 

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  

×