求一字符串匹配算法!规则见内容!!!

huaneww 2004-09-27 11:04:39
各位高手,小弟求一字符串匹配算法,算法规则如下!

规则:有两个输入框,在第一个输入框内输入任意一串不包含‘?’和‘*’字符的字符串!在第二个输入框内输入任意思字符串,可以包含‘?’和‘*’字符!在第二个输入框内的‘?’字符可以匹配第一个输入框内的任意一个字符!‘*’字符可以匹配第一个输入框内的0到任意多个字符!

操作:在第一个输入框内输入一个不包含‘?’和‘*’字符的字符串后,在第二个输入框内输入一个包含‘?’和‘*’字符的字符串来测试这两组字符串是否匹配!!!

请高手帮帮忙!
...全文
217 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
luke5678 2004-09-27
  • 打赏
  • 举报
回复
study!
huaneww 2004-09-27
  • 打赏
  • 举报
回复
哈,收到,正在测试中。。。。
huaneww 2004-09-27
  • 打赏
  • 举报
回复
呃,帅哥,帮人帮到底嘛!谢谢!
王集鹄 2004-09-27
  • 打赏
  • 举报
回复
//测试过,效果还不错~~
function Matchstrings(Source, pattern: string): Boolean;
var
pSource: array[0..255] of Char;
pPattern: array[0..255] of Char;
function MatchPattern(element, pattern: PChar): Boolean;
function IsPatternWild(pattern: PChar): Boolean;
var
t: Integer;
begin
Result := StrScan(pattern, '*') <> nil;
if not Result then Result := StrScan(pattern, '?') <> nil;
end;
begin
if 0 = StrComp(pattern, '*') then
Result := True
else if (element^ = Chr(0)) and (pattern^ <> Chr(0)) then
Result := False
else if element^ = Chr(0) then
Result := True
else
begin
case pattern^ of
'*':
if MatchPattern(element, @pattern[1]) then
Result := True
else
Result := MatchPattern(@element[1], pattern);
'?': Result := MatchPattern(@element[1], @pattern[1]);
else
if element^ = pattern^ then
Result := MatchPattern(@element[1], @pattern[1])
else
Result := False;
end;
end;
end;
begin
StrPCopy(pSource, Source);
StrPCopy(pPattern, pattern);
Result := MatchPattern(pSource, pPattern);
end;


if Matchstrings('Sean Stanley', 'Sean*') then ShowMessage('strings match!');
if Matchstrings('Sean', 'Se?n') then ShowMessage('strings match!');
if not Matchstrings('Sean', 'Se?nn') then ShowMessage('strings don''t match!');
ly_liuyang 2004-09-27
  • 打赏
  • 举报
回复
明白,就象DOS哪个通配符吧?

都不难的,网上找答案了:)

http://lysoft.7u7.net
huaneww 2004-09-27
  • 打赏
  • 举报
回复
晕,不好意思,忘记写了!!

例一:在第一个输入框内输入:‘12112211’
在第二个输入框内输入:‘?211*1' 哪么这一组字符串是匹配的!
例二:在第一个输入框内输入:‘abc1289e'
在第二个输入框内输入:‘a?c*?9?' 哪么这一组字符串是匹配的!

例三:在第一个输入框内输入:‘1211221’
在第二个输入框内输入:‘?*211?1' 哪么这一组字符串是不匹配的!

有点像模糊查询的效果!!只要注意到‘?’和‘*’字符的用法就可以了!!!灵活性有点大!
luke5678 2004-09-27
  • 打赏
  • 举报
回复
有点看不太懂,举个匹配的例子吧!
王集鹄 2004-09-27
  • 打赏
  • 举报
回复
to hellolongbin:这个函数是搜出来的,我只是测试了一下,没用的东西就删掉~~
gangAndgang 2004-09-27
  • 打赏
  • 举报
回复
用正则表达式嘛
hellolongbin 2004-09-27
  • 打赏
  • 举报
回复
to:zswang(伴水清清)(专家门诊清洁工)
你的这个函数:
function IsPatternWild(pattern: PChar): Boolean;
var
t: Integer;
begin
Result := StrScan(pattern, '*') <> nil;
if not Result then Result := StrScan(pattern, '?') <> nil;
end;
后面没有用到,去掉也可以吧

5,930

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧