r/PowerShell • u/Scylenthum • Oct 26 '26
Question Beginner looking for help in a sea of Regex
So I discovered how useful Powershell is about 3 weeks ago and have been playing with it and tweaking my PC with all manner of useless changes. I have reached something that has left me rather stumped even after a fair bit of Googling (AI results disclosure?) and several hundred changes of my 'script'. Posting here is probably like whacking a mole with a warhead but hey why not!
I have been attempting to change a subtitle file with several thousands of lines of text, they all follow the following pattern:
Dialogue: 0,0:00:12.92,0:00:17.34,Default,,0000,0000,0000,,{\pos(540*,420)}Hello world!\N*
I would like to add 140 to all of the first numbers after "{\pos(" (e.g 540 becomes 680.)
Dialogue: 0,0:00:12.92,0:00:17.34,Default,,0000,0000,0000,,{\pos(680*,420)}Hello world!\N*
I have been attempting to do this by loading the .ass text file in (this'll be part of a batch file so I am calling Powershell from CMD), then using the -replace function as follows:
powershell -Command "((Get-Content -path <path to sub.ass> -Raw) -replace '\{\\pos\(\d+,','\{\pos\(${140 + \d+},') | Set-Content -Path <path to sub.ass>"
However this results in the following results for all lines of dialogue:
Dialogue: 0,0:00:12.92,0:00:17.34,Default,,0000,0000,0000,,\{\pos\(,420)}Hello world!\N
This is one of the more promising results where {140 + \d+} simply vanished. Without the $ in front of {140 + \d+} Powershell will simply repeat whatever code I put into into it back at me in place of \d+ like so: {\pos\({140 + \d+},420)}
I suspect that because I am attempting to isolate a number by using the surrounding punctuation the program is reading \d+ as a string rather than an integer.
TLDR: I am attempting to increase the value of the same integer on every line of text by 140 using \d+. \d+ could be any number from 0 to 1540. Not using \d+ is fine but I have no idea what other options are available.
Pointing me in the direction of what I should learn to get this to work could also be great as this is actually a pretty enjoyable program - I just think I went a little too deep too fast with this one.
4
u/Scylenthum Oct 26 '26
I posted this last night before heading to bed and my goodness I am blown away by these responses! I have to go to work, but once I get some free time tonight I will be going over these and seeing how you all got to these methods. Some of these are are for sure more like black magic to me, but I shall try and wrap my head around them. So replies will be incoming!
Once again, thanks everyone!!!
10
u/x0n Oct 26 '26 edited Oct 26 '26 •
Assuming:
We want to replace n with n+140 only where it is preceded by the string "\pos("
results in:
Dialogue: 0,0:00:12.92,0:00:17.34,Default,,0000,0000,0000,,{\pos(680*,420)}Hello world!\N*
This is done by using a "positive look-behind assertion", i.e. there IS something behind the target match that matches a pattern, and then the normal capture of the number. We are then using the dotnet MatchEvaluator overload of Regex.Replace, and powershell will handle creating a delegate for it and bind to a scriptblock. Note that I have to cast the value to [int] to ensure addition doesn't become a string concatenation.
4
u/sigil224 Oct 26 '26
I think I’d recommend trying not to do it in a single line but instead do it line by line (read data file and then foreach line in file…) then use regex to parse the line into named variables and manipulate values there and then recreate the line.
It’ll be slower but then you can debug line by line in your input file, try different things incrementally and see how it all works together.
3
u/ArieHein Oct 26 '26
https://www.w3schools.com/tools/tool_regex.php
3
u/purplemonkeymad Oct 26 '26
Pretty sure this is not something you can do only in regex.
You'll need to match the lines you want, put the value in a capture group to extract it, and either manually or via a second replace put in the new value.
I would recommend not to try to fit that into a single line.
2
u/cbass377 Oct 26 '26
I like to use this website to work up the actual regex. https://regexr.com/
You give it your text, then work on the regex operators until it is selecting what you want.
You will want to set it to PCRE mode.
There are 2 types of scripters, "Everything is a string" and "Everything is an Object" Powershell is for the object type of scripter. I came from Unix / Linux and early windows, building scripts in Perl, so I too am a string type. It is worth the discomfort of working with objects when you work in powershell. That said regex is a powerful tool to find what you want.
You may need to escape the "\" character and maybe the other two punctuation characters.
Hope this helps
1
u/thisisntwhatIsigned Oct 26 '26 edited Oct 26 '26
By treating everything as a string you miss out on the real power of powershell.
Can't check right now, but
Should import as a list of objects where you can the edit only that field in a foreach ($item in $list) loop with a much simpler $item.property -replace command.
0
u/y_Sensei Oct 26 '26
Here's a broader approach that could be expanded to perform other replacement operations inside ASS files, too.
It is based on splitting the different ASS sections up, and processing each section separately, which could make sense since each such section has its own specific format.
-3
u/OddAttention9557 Oct 26 '26
We don't bother trying to wrestle with regex any more, we just ask Claude; he speaks it natively while we lack suitable vocal equipment.