回答編集履歴

2

修正

2019/06/21 11:56

投稿

Zuishin
Zuishin

スコア28660

test CHANGED
@@ -278,7 +278,7 @@
278
278
 
279
279
  }
280
280
 
281
- if (($replace -eq $line) -ne $null) {
281
+ if ($replace -contains $line) {
282
282
 
283
283
  $InputObject -replace $SecondPattern, $Replacement
284
284
 

1

追記

2019/06/21 11:56

投稿

Zuishin
Zuishin

スコア28660

test CHANGED
@@ -169,3 +169,129 @@
169
169
 
170
170
 
171
171
  一番目の引数と二番目の引数が正規表現であることに気を付けてください。
172
+
173
+
174
+
175
+ # 追記
176
+
177
+
178
+
179
+ 二度の仕様追加の要件を満たすもののはずです。これ以降、仕様の追加があるのであれば、この質問は解決済みにして自分で実装してください。それでどうしてもわからなければまた改めて新しい質問を立ててください。
180
+
181
+
182
+
183
+ ```ps1
184
+
185
+ [CmdletBinding()]
186
+
187
+ Param (
188
+
189
+ [Parameter(
190
+
191
+ Mandatory = $true,
192
+
193
+ Position = 0,
194
+
195
+ ValueFromPipelineByPropertyName = $true
196
+
197
+ )]
198
+
199
+ [regex]$FirstPattern,
200
+
201
+
202
+
203
+ [Parameter(
204
+
205
+ Mandatory = $true,
206
+
207
+ Position = 1,
208
+
209
+ ValueFromPipelineByPropertyName = $true
210
+
211
+ )]
212
+
213
+ [regex]$SecondPattern,
214
+
215
+
216
+
217
+ [Parameter(
218
+
219
+ Mandatory = $true,
220
+
221
+ Position = 2,
222
+
223
+ ValueFromPipelineByPropertyName = $true
224
+
225
+ )]
226
+
227
+ [string]$Replacement,
228
+
229
+
230
+
231
+ [Parameter(
232
+
233
+ Mandatory = $false,
234
+
235
+ Position = 3,
236
+
237
+ ValueFromPipelineByPropertyName = $true
238
+
239
+ )]
240
+
241
+ [ValidateRange(0, [int]::MaxValue)]
242
+
243
+ [int]$Count = 5,
244
+
245
+
246
+
247
+ [Parameter(
248
+
249
+ Mandatory = $true,
250
+
251
+ Position = 4,
252
+
253
+ ValueFromPipelineByPropertyName = $true,
254
+
255
+ ValueFromPipeline = $true,
256
+
257
+ ValueFromRemainingArguments = $true
258
+
259
+ )]
260
+
261
+ [string[]]$InputObject
262
+
263
+ )
264
+
265
+ Begin {
266
+
267
+ $replace = @()
268
+
269
+ $line = 1
270
+
271
+ }
272
+
273
+ Process {
274
+
275
+ if ($InputObject -match $FirstPattern) {
276
+
277
+ $replace += ($line + $Count)
278
+
279
+ }
280
+
281
+ if (($replace -eq $line) -ne $null) {
282
+
283
+ $InputObject -replace $SecondPattern, $Replacement
284
+
285
+ $replace = $replace -gt $line
286
+
287
+ } else {
288
+
289
+ $InputObject
290
+
291
+ }
292
+
293
+ $line++
294
+
295
+ }
296
+
297
+ ```