-1

I collect resource dll file names into TStringList by a given mask. 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?

Example 1:

Mask : resources_%s.dll

One of the collected file names : resources_en.dll

Language code : en

Example 2:

Mask : %s_resources.dll

One of the collected file names : eng_resources.dll

Language code : eng

Example 3:

Mask : res-%s.dll

One of the collected file names : res-en.dll

Language code : en

SOLID Developper
  • 672
  • 4
  • 12
  • Do the masks contain at most one (1) instance of `%s`? – Andreas Rejbrand Jul 25 '19 at 23:13
  • @KenWhite It is not so trivial by code as you think at first sight. The regexp solution far from me because of the missing knowledge. But if it is trivial, just give a solution pls and I will accept it (if it works fine as general as I wish) I'd like to ignore to create own code. this is the reason to ask for a built-in functionality. As I realized this page about 'How can I solve this problem' and not 'How can you tease me best' :) – SOLID Developper Jul 25 '19 at 23:17
  • @AndreasRejbrand The number of '%s' is always 1. The main and the optional sub language codes build a whole. (resources_en_us.dll => en_us) – SOLID Developper Jul 25 '19 at 23:21
  • This is not a matter of teasing. It's a matter of this not being a free code writing service. We're more than happy to help, but we expect you to make an effort to solve the problem yourself first. Once you've done so and run into difficulties, you can explain the problem you're having, include the relevant portions of your code, and ask a specific question related to that code, and we can try to help. – Ken White Jul 25 '19 at 23:25
  • The reason I ask is that *in general*, the 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`. 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`. So additional information must be given. – Andreas Rejbrand Jul 25 '19 at 23:28
  • @AndreasRejbrand Let us exclude the repetition (we speak about more or less normal file names) and the number of %s is always 1. – SOLID Developper Jul 25 '19 at 23:35

1 Answers1

2

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.

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. 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. 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.

But you get the idea.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384