Я думаю, что это будет делать:
Прочитать текст как строковый массив и заменить построчно:
(Get-Content 'C:\test.txt') |
ForEach-Object { $_ -replace '(id\s*=\s*[^\s]+)', 'id=1234' } |
Add-Content -Path 'C:\test_updated.txt'
или прочитайте текст как одну строку и выполните многострочную замену ((?m)
)
(Get-Content C:\test.txt -Raw) -replace '(?m)(id\s*=\s*[^\s]+)', 'id=1234' |
Set-Content -Path 'C:\test_updated.txt'
Я настоятельно рекомендую использовать новое имя файла для выходного файла, чтобы не перезаписывать оригинал.
В обоих случаях код возвращает:
text over here
id=1234
text over here: id=1234
text
id=1234
id=1234 text
id=1234 text
Сведения о регулярном выражении
( Match the regular expression below and capture its match into backreference number 1
id Match the characters “id” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
= Match the character “=” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[^\s] Match any character that is NOT a “A whitespace character (spaces, tabs, line breaks, etc.)”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)