1

I need to change every multi-line comments that looks like this:

/*
Some
multiline
comment
*/

,into this:

#
#
#
#
#

The number of rows must be the same. How can I achieve this in one expression using Regex from .NET?

  • What are the rules for multi-line comments in the source file? I.e. can they be nested? – Damien_The_Unbeliever Aug 25 '15 at 10:31
  • @Damien_The_Unbeliever: Your question is valid, though I haven't seen any language with nested comment... – nhahtdh Aug 25 '15 at 10:32
  • @nhahtdh - I'm unable to present any examples off hand. I just have a nagging remembrance of such things. They may, in fact, have been "toy" languages used during my CS course. – Damien_The_Unbeliever Aug 25 '15 at 10:35
  • 1
    @nhahtdh - ah, here we go - [OCaml](https://ocaml.org/learn/tutorials/basics.html) – Damien_The_Unbeliever Aug 25 '15 at 10:37
  • 1
    Hmm... Something like this? https://regex101.com/r/rM0cZ8/3 (adapted from http://stackoverflow.com/questions/15268504/collapse-and-capture-a-repeating-pattern-in-a-single-regex-expression/15418942#15418942, first construction). It's a one-regex solution, though I wouldn't recommend it. – nhahtdh Aug 25 '15 at 10:50
  • Hi, the comments came from T-SQL code and they cannot be nested. I need to clean the code from any comments but the number of lines of code must be unchanged so I can't just remove the comments rather replace them with empty rows. – marcin.zamorski Aug 25 '15 at 10:56
  • @nhahtdh - That works too. Thanks! :-) – marcin.zamorski Aug 25 '15 at 11:14

1 Answers1

1

Assuming you do not have comment-like strings in string literals and that comments can't be nested (since the string comes from T-SQL), you can try

var rx = new Regex(@"/\*(?s:.*?)\*/");
var txt = @"/*
Some
multiline
comment
*/";
var replaced = rx.Replace(txt, m => String.Concat(Enumerable.Repeat("#\r\n", m.Value.Split(new string[] {"\r\n"}, StringSplitOptions.None).Count())).Trim());

Result:

enter image description here

The /\*(?s:.*?)\*/ regex matches any text between /* and */. The logic is that we get the whole match, split it with linebreaks, and then build a replacement string based on the number of lines.

If you want to match just the lines that are all-comments, you can use the following regex (see demo):

(?m)^\s*/\*(?s:.*?)\*/\s*$
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Glad it worked for you. Please also consider upvoting if my answer proved helpful to you. See [How to upvote on Stack Overflow?](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow) – Wiktor Stribiżew Aug 25 '15 at 11:14