gpt4 book ai didi

delphi - 如何在字符串中搜索子字符串的多种变体

转载 作者:行者123 更新时间:2023-12-03 18:00:25 25 4
gpt4 key购买 nike

我正在尝试在字符串中搜索子字符串,但我想应该有比这更有效的方法..

      //search for volume
if AnsiContainsStr(SearchString, 'v1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'V1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'Volume1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'Volume 1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'Vol1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'vol1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'Vol 1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'vol 1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'Vol.1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'vol.1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'Vol. 1') then
Volume := '1';
if AnsiContainsStr(SearchString, 'vol. 1') then
Volume := '1';


if AnsiContainsStr(SearchString, 'v2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'V2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'Volume2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'Volume 2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'Vol2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'vol2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'Vol 2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'vol 2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'Vol.2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'vol.2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'Vol. 2') then
Volume := '2';
if AnsiContainsStr(SearchString, 'vol. 2') then
Volume := '2';

最佳答案

因为你用 XE2 标记了这个,你可以使用正则表达式来轻松匹配

  var
Regex: String;
begin
Regex := '^[v](ol\.?|olume)?\s*(1|\.\s*1)$';
if TRegEx.IsMatch(SearchString, Regex, [roIgnoreCase]) then
Volume := '1'
Regex := '^[v](ol\.?|olume)?\s*(2|\.\s*2)$';
if TRegEx.IsMatch(SearchString, Regex, [roIgnoreCase]) then
Volume := '2'
end;

现在,我不是设计正则表达式的最佳人选,但我测试了上面的正则表达式,它似乎匹配您的所有变体(也许其他人可以想出一个更简洁的)。

关于delphi - 如何在字符串中搜索子字符串的多种变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11658731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com