0

I am trying to achieve this but it is working in Notepad++ but not in PowerShell+regex

File: 1.txt

1
2
3
4

PS:

(Get-Content E:\1.txt).replace("(?ms)1((?:.*\r?\n?)*)4", "success") | Set-Content E:\2.txt

I have tried with ?ms, ?s and ?m.

Any help?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • `Get-Content E:\1.txt -Raw` and no need for any `m` or `s` – Wiktor Stribiżew Feb 18 '20 at 13:06
  • Still the same the content of the file is 1234.. what i am trying to achieve is I know "1" and "4" with any text or line inbetween to be replaced with word success (Get-Content E:\1.txt -raw).replace("1((?:.*\r?\n?)*)4", "success") | Set-Content E:\2.txt – Manickam Chockalingam Feb 18 '20 at 13:16
  • Oh, you used `String.Replace`. Use `-replace`. Another dupe: https://stackoverflow.com/questions/10184156/whats-the-difference-between-replace-and-replace-in-powershell – Wiktor Stribiżew Feb 18 '20 at 13:20
  • sample
    Other My Objective is to remove from
    sample... till...
    other PS Script looks like this $start = '
    sample' $end = '
    Other' $regexpattern = '((?:.*\r?\n?)*)' $searchString = $start+$regexpattern+$end $a -replace '$searchString','mytext' | out-file "d:\a22.html" this it just hangs.. Any idea on where i am getting it wrong
    – Manickam Chockalingam Feb 19 '20 at 07:08
  • The regex to match from `1` to `4` is `(?s)1.*?4` – Wiktor Stribiżew Feb 19 '20 at 11:26

0 Answers0