I am trying to parse the following line:
"\#" TEST #comment hello world
In my input, the #comment always comes at the end of the line. There may or may not be a comment, but if there is, its always in the end of the line.
I used the following Regex to parse it:
(\#.+)?
I have the RegexOption.RightToLeft on. I expected it to pull #comment hello world. But instead it is pulling "#" TEST #comment hello world"
Why is my Regex expression not pulling the right thing and what is the valid Regex expression I need to make it pull correctly?
I think you’ll find too many edge cases when trying to pull this off with regular expressions. Dealing with the quotes is what really complicates things, not to mention escape characters.
A procedural solution is not complicated, and will be faster and easier to modify as needs dictate. Note that I don’t know what the escape characters should be in your example, but you could certainly add that to the algorithm…
This example strips the comments away from the
CodeSnippet. I assumed that’s what you were after.Here’s the output:
As I said, you’ll probably need to add escape characters to the algorithm. But this is a good starting point.