1

I'm writing a parser for Windows driver inf files. They are similar to ini files, with some odd things about them.

The part I'm struggling with right now is that if a line contains a ; anywhere in the line, it's considered a comment.

However, if it's surrounded by ", then it's a string. Anything inside " is literal and an actual string.

And to make things more interesting, if you want to have a literal " inside a string, you need to precede it with another ".

So in other words:

"This is a string" ;This is a comment
"This is a string; which contains a semicolon"
"This is a string; which contains a semicolon" ;And a comment
"This is a string; which contains a semicolon and a double quote """
"These are", "two strings with ""double quotes""" ;And a comment

What's the easiest way to determine whether there is a comment in the line and also to extract the strings from the lines?

I imagine this might be regex, but not sure if that's the best way so I'm open to ideas.

cogumel0
  • 2,430
  • 5
  • 29
  • 45
  • If you're going to mark it as a duplicate, at least mark it as a duplicate of something that's even slightly related to my question. There's nothing on that question/answer that explains how to determine if it is a comment or part of an actual string. – cogumel0 Jul 16 '17 at 19:55
  • It is actually _**partially**_ related. The answer in that question uses Visual Basic's `TextFieldParser` class which includes a [**`CommentTokens` property**](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.commenttokens.aspx) for defining comment characters. – Visual Vincent Jul 16 '17 at 20:03
  • @cogumel0 Check out this link on dotnet fiddle. hope it helps -- https://dotnetfiddle.net/5VSDCf -- i put the lines in a file on my PC – Glenn Ferrie Jul 16 '17 at 20:07
  • @GlennFerrie that doesn't work well. Your logic is that if a `;` appears *after* the last `"` then it must be a comment. But the opposite might also be true. Imagine the following line `; This comment contains a double quote ". It is still ONLY a comment.` – cogumel0 Jul 16 '17 at 20:42

0 Answers0