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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex and multiline replacement pattern, take 2

Status
Not open for further replies.

gacaccia

Technical User
May 15, 2002
258
US
so i've basically got a code file with comments where i want to remove the comments using a regular expression. the comments are identified with /* and */, which can span multiple lines. the final solution from a prior post was to use:

Code:
[Regex]::Replace("/\*.*?\*/","")

unfortunately, the result from this removes all new line characters in the code file. i'm not sure if this is an issue with powershell. i tried this pattern on a regexp test web site (non-powershell code) and the results it provided retained new line characters for non-comment sections. any thoughts?

thanks,

glenn
 
found a solution.

Code:
$OFS = "`r"
$fileContent = get-content $codeFileName
$fileContent = [Regex]::Replace("/\*.*?\*/","")
$fileContent = $fileContent.Split("`r")

my understanding is that regex collapses the array of strings from get-content into a single string. by setting $OFS to insert a return character after each array element at time of collapse, you can then recreate the array after the replace by splitting the string back out to the individual elements as delimited by the return character.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top