Finding a pattern in a string using a pattern in Delphi?

I used the HYPERSTR library for string processing. Now I am using the new Delphi. I need to find a pattern in a string, for example, the old function function IsMatchEx(const Source, Search:AnsiString; var Start:integer) : Integer; . Actually, I don’t need the value of the result, I just want to know if the template matches the string or not.

My old code (returns TRUE):

 var StartPos: integer; FoundPos: integer; begin StartPos := 1; FoundPos := IsMatchEx('abcdef', 'abcd?f', StartPos); if FoundPos > 0 then showmessage('match'); end; 

I see that Delphi XE has TRegEx, but I don’t understand how to use it.

This code does not return TRUE:

  if TRegEx.IsMatch('abcdef', 'abcd?f') then showmessage('match'); 

I also got the same result when using MatchesMask .

Thanks.

+6
source share
3 answers

if? represent one character:

  if TRegEx.IsMatch('abcdef', 'abcd.f') then showmessage('match'); 

if? represent any sting:

  if TRegEx.IsMatch('abcdef', 'abcd.*f') then showmessage('match'); 

It does not have XE, so it has not been tested.

+4
source

Regular expression syntax is different.? and * have different meanings. See http://www.regular-expressions.info/tutorial.html for an excellent introduction to regular expressions. You should use something like abcd [az] f or abcd \ wf or even a different syntax, depending on what you would like to match.

+9
source

You can use TMask to match wildchar:

 TMask *m = new TMask("String to check"); bool isMatch = m->Matches("string to*"); delete m; 

isMatch = true (C ++ Builder code just translates to Pascal)

0
source

Source: https://habr.com/ru/post/888014/


All Articles