# Replace nth regex match with sequential number

**URL:** <https://forums.powershell.org/t/replace-nth-regex-match-with-sequential-number/18513>\
**Category:** PowerShell Help\
**Created:** [February 6, 2022, 12:47am UTC](https://forums.powershell.org/t/replace-nth-regex-match-with-sequential-number/18513 "2022-02-06T00:47:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant-harrington](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@grant-harrington](https://forums.powershell.org/u/grant-harrington)\
**Post date:** [February 6, 2022, 12:47am UTC](https://forums.powershell.org/t/replace-nth-regex-match-with-sequential-number/18513/1 "2022-02-06T00:47:54Z")

</div>

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.

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 6, 2022, 12:50am UTC](https://forums.powershell.org/t/replace-nth-regex-match-with-sequential-number/18513/2 "2022-02-06T00:50:08Z")

</div>

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

This should do the trick I guess:

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![grant-harrington](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@grant-harrington](https://forums.powershell.org/u/grant-harrington)\
**Post date:** [February 6, 2022, 12:55am UTC](https://forums.powershell.org/t/replace-nth-regex-match-with-sequential-number/18513/3 "2022-02-06T00:55:06Z")

</div>

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)

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 6, 2022, 12:58am UTC](https://forums.powershell.org/t/replace-nth-regex-match-with-sequential-number/18513/4 "2022-02-06T00:58:17Z")

</div>

> [@grant-harrington](#):
>
> Thanks. In example code below, I want

Cool. Thanks.

The code suggestion I posted actually works. Try it. 😉

---

<div class="post-metadata">

**Author:** ![grant-harrington](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@grant-harrington](https://forums.powershell.org/u/grant-harrington)\
**Post date:** [February 6, 2022, 1:01am UTC](https://forums.powershell.org/t/replace-nth-regex-match-with-sequential-number/18513/5 "2022-02-06T01:01:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 6, 2022, 1:02am UTC](https://forums.powershell.org/t/replace-nth-regex-match-with-sequential-number/18513/6 "2022-02-06T01:02:29Z")

</div>

… glad you like it. 😉 👍🏽

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:19pm UTC](https://forums.powershell.org/t/replace-nth-regex-match-with-sequential-number/18513/7 "2024-05-16T20:19:34Z")

</div>


