0

I'm having some trouble to remove block of comments that looks like this:

//JR-0001-//
//iErr :=  0;
// IF NOT TSUtil.ReadStatementTransactions(TRUE,iErr) THEN BEGIN
//  IF (iErr <> 0) THEN BEGIN
//    IF NOT PosConfirm(Text223,FALSE) THEN
//      EXIT;
//  END;
//END;
//JR-0001+//

JR-0001-: this is the beginning of a comment block

JR-0001+: this is the end of a comment block

I was able to remove single line comments (that starts with "//JR-", but I'm stuck with removing a block of comments.

Any help is appreciated.

1
  • 1
    Welcome to stackoverflow! You should show the code you have already, so people can help you with that. Otherwise it turns into a code writing site instead of a learning site. Commented Mar 17, 2020 at 9:25

2 Answers 2

2

One way to do this would be a loop.

For each line of the input file:

  1. if the line looks like a "start comment" line, set a "is comment" flag
  2. if the flag is currently not set, output the line
  3. if the line line looks like an "end comment", unset the flag

ForEach-Object is a loop and can express this as follows

$isComment = $false                                             # prepare flag variable

$inputLines | ForEach-Object {
    if ($_ -match '^//JR-\d*-//')  { $isComment = $true  }      # enable flag
    if (-not $isComment) { $_ }                                 # output if flag is not set
    if ($_ -match '^//JR-\d*\+//') { $isComment = $false }      # disable flag
}

The regex is

^                    # start of string
//                   # two slashes
JR-                  # "JR-"
\d*                  # digits (0-n repetitions)
-                    # a minus sign (a plus sign in the second expression)
//                   # two slashes
Sign up to request clarification or add additional context in comments.

2 Comments

This works!! Thank you for the idea! I can now remove blocks of comment as well as single line comments.
@JaniceChew Good to hear. I hope the code makes sense and you can apply the principle to similar tasks in the future. :)
0

You mean something like this...

$MyComment = @'
MyCode
BeginMyComment

//JR-0001-//
//iErr :=  0;
// IF NOT TSUtil.ReadStatementTransactions(TRUE,iErr) THEN BEGIN
//  IF (iErr <> 0) THEN BEGIN
//    IF NOT PosConfirm(Text223,FALSE) THEN
//      EXIT;
//  END;
//END;
//JR-0001+// 

EndMyComment
MyCode

'@
 # Grab and remove all content between two string pattern and the string pattern
$MyComment -replace '//JR-\d{4}-//(?s)(.*)\+//'

<#
# Results

MyCode
BeginMyComment



EndMyComment
MyCode
#>

8 Comments

That does not work with what e.g. Get-Content normally returns. Also, please always post regex with an explanation.
The OP never said he was scanning for content in files on his filesystem. You made that assumption and thus the loop would be prudent. He could have been in an editor like VScode, and maybe wanted to remove comments from the editor pane. So, until the OP is more specific about his use case, both are valid for different reasons. That RegEx is what I put together to remove unneeded comments when using the PowerShell ISE and VSCode, daily. I did not explain, as he OP seems to indicate that he is using RegEx/Select-String, etc, for his use case and you're don't really explain yours either.
Damn, you're right, I explained my code but not my regex. *facepalm*
;-} --- yeppers, I've done stuff in the past like this as well, and I've seen others have it happen to them. Yet, the arguments can often be inconsistent. As many long-time players here like yourself and others do things, and If I see a response/style pattern, I kind of jump on broad with that, sometimes, but then I get panned for things, other long-term players, etc. do regularly. So, you know, the old adage, 6 on one hand/half-dozen on the other. After decades in the industry, the only constant is change.
FWIW, a hint should not be missing that this kind of regex-block-replace approach will require Get-Content -Raw, otherwise you get an array of lines and this will not work.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.