在Delphi中使用通配符在字符串中搜索模式? [英] Search pattern in string using wildcard in Delphi?
问题描述
我以前使用HYPERSTR库进行字符串处理例程。现在,我使用较新的Delphi。我需要搜索字符串中的模式,例如,旧函数为 function IsMatchEx(const Source,Search:AnsiString; var Start:integer):Integer;
。其实我不需要结果值,我只是想知道模式是否与字符串匹配。
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.
我的旧代码(返回TRUE):
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;
我看到Delphi XE具有TRegEx,但我仍然不知道如何使用它。
I see that Delphi XE has TRegEx but I stil don't understand to use it.
这些代码未返回TRUE:
These code doesn't return TRUE :
if TRegEx.IsMatch('abcdef', 'abcd?f') then
showmessage('match');
使用 MatchesMask
时,我也得到了相同的结果。
I also got same result when using MatchesMask
.
谢谢。
推荐答案
如果?代表单个字符:
if TRegEx.IsMatch('abcdef', 'abcd.f') then
showmessage('match');
如果?表示任何字符串:
if ? represent any sting:
if TRegEx.IsMatch('abcdef', 'abcd.*f') then
showmessage('match');
没有XE,所以还没有测试。
Don't have XE so haven't tested.
这篇关于在Delphi中使用通配符在字符串中搜索模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!