[英]Subtract a mask from a string in Delphi 10.3
I collect resource dll file names into TStringList
by a given mask.我通过给定的掩码将资源 dll 文件名收集到TStringList
。 It works fine to that point.它工作正常。 After that I want to collect the language codes contained by the file names.之后我想收集文件名包含的语言代码。 Is there any built-in
function or a well formed regexp
to get the right string section depending on the mask?是否有任何built-in
函数或格式良好的正则regexp
来根据掩码获得正确的字符串部分?
Example 1:示例 1:
Mask : resources_%s.dll掩码:resources_%s.dll
One of the collected file names : resources_en.dll收集的文件名之一:resources_en.dll
Language code : en语言代码 : zh
Example 2:示例 2:
Mask : %s_resources.dll掩码:%s_resources.dll
One of the collected file names : eng_resources.dll收集的文件名之一:eng_resources.dll
Language code : eng语言代码 : eng
Example 3:示例 3:
Mask : res-%s.dll掩码 : res-%s.dll
One of the collected file names : res-en.dll收集的文件名之一:res-en.dll
Language code : en语言代码 : zh
In general , this problem is mathematically impossible to solve.一般来说,这个问题在数学上是不可能解决的。
For instance, consider the mask alpha_%s_beta_%s_gamma
and the output alpha_cat_beta_beta_dog_gamma
.例如,考虑掩码alpha_%s_beta_%s_gamma
和输出alpha_cat_beta_beta_dog_gamma
。
It is possible that the first value is cat
and the second is beta_dog
, but it could also be the case that the first value is cat_beta
and the second is dog
.第一个值可能是cat
,第二个值是beta_dog
,但也可能是第一个值是cat_beta
而第二个值是dog
。 So additional information must be given.所以必须提供额外的信息。
If, for instance, you know that the mask always contains exactly one (1) instance of %s
, then the problem is very easy to solve.例如,如果您知道掩码始终包含%s
一 (1) 个实例,那么问题就很容易解决。 For instance (omitting all error checking):例如(省略所有错误检查):
function GetValueFromMask(const AMask, AText: string): string;
var
p: Integer;
begin
p := Pos('%s', AMask);
Result := Copy(AText, p, 2 + AText.Length - AMask.Length);
end;
Of course, you would never use that code in production.当然,您永远不会在生产中使用该代码。 You would add error checking, making sure you handle AText
not matching AMask
, AMask
containing zero or >1 %s
, etc.您将添加错误检查,确保处理AText
不匹配AMask
、包含零或 >1 %s
AMask
等。
But you get the idea.但是你明白了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.