Chris1701 0 Posted November 23, 2023 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
Lajos Juhász 293 Posted November 23, 2023 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
Lajos Juhász 293 Posted November 23, 2023 If you want to compare against file mask you should use https://docwiki.embarcadero.com/Libraries/Alexandria/en/System.Masks.MatchesMask. For example: uses System.Masks; procedure TMainForm.TestClick(Sender: TObject); begin if MatchesMask(edText.Text, EdPattern.Text) then lbData.Insert( 0, 'True' ) Else lbData.Insert( 0, 'False' ); end; 1 Share this post Link to post