开发者

Search pattern in string using wildcard in Delphi?

I used to use HYPERSTR library for string processing routine. Now I use newer Delphi. I need to search a pattern in a string, for example the old function is function IsMatchEx(const Source, Search:AnsiString; var Start:integer) : Integer;. Actually I don't need the result value, I just wanna know if the pattern match with 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 stil don't understand to use it.

These code doesn't return TRUE :

  if TRegEx.IsMatch('abcdef', 'abcd?f') then
    showme开发者_StackOverflowssage('match');

I also got same result when using MatchesMask.

Thanks.


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 would use something alike abcd[a-z]f or abcd\wf, or even other syntax, depending on what you would like to match.


if ? represent a single character:

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

if ? represent any sting:

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

Don't have XE so haven't tested.


You can use TMask for wildchar matching:

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

isMatch = true (C++Builder code is simply translable in Pascal)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜