【发布时间】:2011-08-24 18:32:26
【问题描述】:
我曾经使用 HYPERSTR 库进行字符串处理例程。现在我使用较新的德尔福。我需要在字符串中搜索模式,例如旧函数是function IsMatchEx(const Source, Search:AnsiString; var Start:integer) : Integer;。其实我不需要结果值,我只想知道模式是否与字符串匹配。
我的旧代码(返回 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,但我还是不明白如何使用它。
这些代码不返回 TRUE:
if TRegEx.IsMatch('abcdef', 'abcd?f') then
showmessage('match');
我在使用MatchesMask时也得到了同样的结果。
谢谢。
【问题讨论】:
-
认为你使用'.'匹配单个字符。
-
真的吗? MatchesMask 应该起作用了。您确定您的测试有效吗?
-
@Rob,我的意思是 MatchesMask 在我的真实案例中不起作用。通过使用点,我的问题已经解决。抱歉回复晚了,因为我必须修复一些代码,以便我可以尝试 MGH 的答案。
标签: delphi pattern-matching delphi-xe