-1

Im trying for a while to split code file (treated as text file) by the comments that in it. For example, for the input:

// Hi guys, I am trying to get some help here.
// I really tried to do this alone.
/* But i still search for help
in our bes friend Google.*/

I expect to get the output:

Hi guys, I am trying to get some help here.
I really tried to do this alone.
But i still search for help in our bes friend Google.

so basiclly i want to recognize that there is a comments in the file (by the symbols // and /* */) and enter the comments in a list (each comment in a differend cell).

I am trying to do so by the code line: codeFile.Split('//', '/', '/'); But with no success. As well, since it is possible for multi-line comment when using the "/* */" symbol, how can i enter the intire string between them to my list since I am run over the file by the lines?

Thanks in advence.

a43ixus
  • 49
  • 7
  • See also https://stackoverflow.com/questions/49843885/roslyn-get-grouped-single-line-comments for inspiration on how you might take advantage of the Roslyn environment to solve this problem. – Peter Duniho Jun 16 '21 at 02:00

1 Answers1

0

I would do something like :

  • Read your file line per line
  • Check if line match with Regex for your different criterias.
  • Build a new string (with line break if needed) using the information you got from your checking. You will be able to handle the multi line factor.

Hope this could be helpful.

Benliam12
  • 81
  • 5
  • When I am trying to use regex like: string singelCommentPattern = "[\s\S]([^a-zA-Z/][^\n\r][a-zA-Z/]?//.*[/]*)"; the compiler gives me an error of unrecognized escape sequence – a43ixus Jun 16 '21 at 02:43
  • Your "//" is a regex thing, change it to \/\/. (back slash in front of front slash). Also, consider checking your regex structure using https://regexr.com/ to validate your expression. – Benliam12 Jun 16 '21 at 14:50
  • using the Regex was quit hard work but it paid of! it runs well now. – a43ixus Jun 24 '21 at 22:52