Replace nth regex match with sequential number

I have a text file (.vtt) that has GUIDs, DateStamp,Captions. I’m trying to replace the GUIDs with sequential numbers. What I have is working, but it’s replacing the GUID value with the index line of the given document (If first GUID is on line 28, the replaced number is 28, if second GUID is on line 54, the replaced number is 54).

My goal is to replace the “first” GUID (line 28) with 1, the “second” GUID (line 54) with 2 and so on.

$guidRegex = '(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$'
$i = 1

$replaceNote | ForEach-Object {
$Number = $i
$_ -replace "$guidRegex","$Number"
$i++
} | Out-File C:\Temp\guidexport.txt -Append

Could you please share some sample data to play with? (formatted as code please :wink: )

This should do the trick I guess:

$guidRegex = '(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$'
$i = 1

$replaceNote |
ForEach-Object {
    if ($_ -match $guidRegex) {
        $_ -replace $guidRegex, $i
        $i++
    }
    else {
        $_
    }
} | 
Out-File C:\Temp\guidexport.txt

Thanks. In example code below, I want
GUID 28f… replaced with 1 (current result is 9)
GUID 571… replaced with 2 (current result is 16)
GUID 9f3… replaced with 3 and so on… (current result is 23)





28f9d656-9bb2-44a5-80d1-2608803525ce
00:00:11.380 --> 00:00:16.077
I'm going to show you how to
synchronize a private teams



571fa561-bb07-4aae-bc33-0e24cea26291
00:00:16.077 --> 00:00:21.201
channel to your File Explorer in
a fashion similar to what you're



9f32361c-8cf7-48b3-8fcb-047d0f38ad4c
00:00:21.201 --> 00:00:25.471
used to with under this PC and
the your mapped drive



8f9b3bdb-da3b-47c1-925e-edbd059e2040
00:00:26.430 --> 00:00:28.734
In this example, I'm looking at

Cool. Thanks.

The code suggestion I posted actually works. Try it. :wink:

Sure does. Marked as Solution. Thanks for the quick turn around!

… glad you like it. :wink: :+1:t4: