I want a strange one.

There is a very large array of strings. All these lines fit well under a certain set of templates.

"Red Hat Enterprise Linux Server release 6.0 (Santiago)", "Microsoft Windows [Version 6.1.7601]", "HP-UX bi2 B.11.31 U ia64 3768995465 unlimited-user license", "Microsoft Windows [Version 6.3.9600]", "SUSE Linux Enterprise Server 11 (x86_64)", "Red Hat Enterprise Linux Server release 5.5 (Tikanga)", "HP-UX cfb B.11.31 U ia64 3919763569 unlimited-user license", "Microsoft Windows [Version 6.1.7601]", "SUSE Linux Enterprise Server 11 (x86_64)", "Microsoft Windows [Version 6.1.7601]", "SUSE Linux Enterprise Server 11 (x86_64)", "HP-UX mappn B.11.31 U ia64 1709713642 unlimited-user license", "HP-UX appn B.11.31 U ia64 1709713642 unlimited-user license", "SUSE Linux Enterprise Server 11 (x86_64)", "Microsoft Windows [Version 6.3.9600]", ..... 

That is, for example, Red Hat is described by such a pattern (for example, printf)

 $vendor = "Red Hat Enterprise Linux Server"; $version = 6; $patchLevel = 0; $versionName = "Santiago"; printf("%s release %d.%d (%s)", $vendor, $version, $patchLevel, $versionName); 

Red Hat Enterprise Linux Server release 6.0 (Santiago)

So, I want a sort of printf on the contrary, to break a line into parts from a template.

It is clear that this is easily done with the help of regulars, but the list is really very large and, in general, will pull quite a lot of strapping to analyze the result.

Any thoughts where to look?

  • If it is easier and faster, then you can try to look in the direction of the sscanf function. Otherwise, you can get stuck and write a parser on the state machines, the parser lol! If the pattern is exactly simple and well-known, then the regulars here are the most. I see no reason to refuse them. - Lexx918
  • That's about sscanf, make a reply pliz, for what you need - rjhdby

1 answer 1

With some reservations, the sscanf function is suitable.

Since the key s stops reading on any whitespace character (and only on it!), For example, for such a string

 Microsoft Windows [Version 6.1.7601] 

such a template will not work:

 %s %s [%s %s] 

Because the last %s capture all the characters to the end of the line, including the closing square bracket. It is necessary to forcibly break the string into numbers and pray that the letter does not crept into the minor versions. This is how it will work:

 %s %s [%s %i.%i.%i] 

But to disassemble such a line without a file completion, in principle, it will not work:

 Microsoft Windows [Version 6.1.7601a]