Reputation: 1243
I have a regular expression that works perfectly in Sublime text, or other text editors, and it does exactly what I need it to do.
Here is the regular expression
(?sm),"([\w\W]*?)Date completed:
The problem is, in PowerShell, it just doesn't do anything.
This is my little script
$text = Get-Content c:\Tools\export.csv
$text -replace '(?sm),"([\w\W]*?)Date completed: ','REPLACED' | Out-File output.csv
If I replace the regular expression by plain text, it works great. So what is it that it does not like in my regular expression?
Thanks!
Upvotes: 0
Views: 302
Reputation: 1243
Based on the info I got from the people who answered, here is how I was able to accomplish what I needed to do for the regular expression
$text = (Get-Content c:\Tools\export.csv) | out-string
$regex = "(?sm),""\s.*?Date completed: "
$replace = ',"'
$output = [regex]::replace($text,$regex,$replace)
$output | Out-file c:\Tools\output.csv -Encoding ascii
Upvotes: 0
Reputation: 46710
Regardless of what you are trying to match against when you use single and multimode those are supposed to work on multi-lined strings. Get-Content
by default will return a string array. -replace
functions as an array operator and will run that pattern against each line individually.
So for starters make $text
one string.
$text = Get-Content c:\Tools\export.csv | Out-String
Or if you have at least PowerShell 3.0
$text = Get-Content c:\Tools\export.csv -Raw
Upvotes: 1