Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regexp and multiline replace pattern 1

Status
Not open for further replies.

gacaccia

Technical User
May 15, 2002
258
US
is it possible to use a regular expression to remove all comments from a code file? for example, if i have a file with:

Code:
here is some code

/* here
are some
comments */

here is some code

is there a way to specify a regexp pattern such that it will remove the comment. i wanted to do something like:

Code:
$codeFile -replace "/\*.*\*/",""

but that doesn't work. thoughts?

thanks,

glenn
 
The pattern you have has two problems:

1) It's greedy. It would match from the beginning of the first multiline comment to the end of the last. You need to make it non-greedy (.*?)

2) the dot matches any character EXCEPT newline. To make powershell include newline in dot matches, you need to turn on 'single line' mode. Prefix the pattern with "(?s)"

so your pattern should be "(?s)/\*.*?\*/
 
i tried that and it still didn't work. specifically...

Code:
$codeFile -replace "(?s)/\*.*?\*/",""

...where $codeFile contains the text of the file (using get-content), the comments still remain.
 
You're using -replace which is expecting a string replace, not a regexp replace.

If you want to use regex replace:


Code:
$pattern = "(?s)/\*.*?\*/"
$codefile = get-content -path [i]thefile.cs[/i]
$stripped = [Regex]::Replace( $codeFile, $pattern, "" )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top