Skip to main content
SubtitleMatrix

Regex Replace in Subtitles

Text and stylingRegular expression replace
Or paste subtitle text

Options

A regular expression, matched against the text of each cue. Every match is replaced. The two pages in this section share the box: one opens it empty for a pattern you already have, the other starts it off with a pattern you can edit either way.

What each match becomes. Leave it empty to delete every match. Unlike the literal find-and-replace page, a dollar sign here is special: $1 stands for the first bracketed group of the match, $& for the whole match, and $$ for a literal dollar sign.

The letters the pattern is compiled with. g and u are on by default: u makes the pattern read the text a character at a time rather than a code unit at a time, which is what makes \p{L} and friends mean what they say. Add i to ignore case, m for line anchors, s to let a dot match a line break. Clear the box to compile with no flags at all. Any other letter is refused rather than ignored, because a pattern run under flags you did not ask for is a pattern that quietly means something else.

Which frame rate the timings are counted in. On the format pages this is what MicroDVD needs, because it stores timing as frame numbers. On the frame rate pages it is the rate the file was timed against — the version the subtitles were made for — and the page's second box takes the rate of the video you are going to use them with. 23.976, 24, 25, 29.97, 30, 50, 59.94 and 60 are the common ones. Getting it wrong shifts the whole timeline, and on the frame rate pages it shifts it by a ratio rather than by a fixed amount.

Fill in Pattern to run.

This page rewrites the text inside subtitle cues with a pattern you write. A pattern is not a fixed string: `\d+` means any run of digits, `(cat|dog)` means either word, and a dot stands for any character at all. Every match, in every cue, is replaced in one pass, and the page reports how many places changed. Only the text moves — every start and end time is carried across exactly as it was.

The replacement box is not a fixed string either. A dollar sign in it is special: `$1` stands for whatever the first bracketed group of the match caught, `$&` for the whole match, and `$$` for a literal dollar sign. That is what lets one rule tidy thousands of different lines — deleting the stray space before a full stop while keeping the punctuation, because the punctuation is what the group caught.

Patterns run with `g` and `u` on. `g` means find every match rather than only the first; `u` makes the pattern read the text a character at a time instead of a code unit at a time, which is what makes `\p{L}` mean a letter. Add `i` to ignore case, `m` to let anchors work line by line, `s` to let a dot match a line break. Clear the flags box to run with none of them. Any other letter refuses the whole run rather than being ignored, because a pattern run under flags you did not ask for is a pattern that quietly means something else.

Inline styling is carried along whenever there is an honest place for it to sit: a tag shifts when the edit happens outside it and stretches when the edit happens inside it. When a match crosses a tag's edge the markup is dropped and counted in the loss report, because the alternative is italics wrapped around the wrong words. Nothing is uploaded, each file is written back in its own format, and a whole season can go through the queue at once.

Examples

Keeping the punctuation with a capture group

Input

1
00:00:01,000 --> 00:00:02,000
We stopped , and waited .

2
00:00:03,000 --> 00:00:04,000
Right !

Output

1
00:00:01,000 --> 00:00:02,000
We stopped, and waited.

2
00:00:03,000 --> 00:00:04,000
Right!

The pattern is a run of spaces followed by one of . , ! ? — and the bracket makes that punctuation the first group, so `$1` puts it back. Three matches, three changes. A literal search cannot express this at all: it can only turn one fixed string into another fixed string.

One pattern, both spellings

Input

The Colour of the sky.
colourless and calm.

Output

The color of the sky.
colorless and calm.

With `i` in the flags box, `colour` catches the capitalised spelling as well. Note what the flag does not do: the capital that started the line is gone, because a pattern has no idea what a word is doing in a sentence. If the capital matters, that is a second rule.

A match that crosses the edge of an italic run

Input

1
00:00:01,000 --> 00:00:04,000
<i>Hello there</i>, welcome aboard

Output

1
00:00:01,000 --> 00:00:04,000
Hello there. Welcome aboard

The match begins inside the italics and ends outside them, so half of what the markup covered is gone and there is no correct place left for it. It is dropped and counted rather than left wrapped around the wrong letters.

Frequently asked questions

How do I keep part of what the pattern matched?

Put brackets around that part in the pattern and refer to it as `$1` in the replacement — `$2` for the second group, and so on. The pattern `\s+([.,!?])` with the replacement `$1` deletes the space and keeps the punctuation, which is exactly the job a fixed string cannot do. `$&` stands for the whole match, and `$$` for a dollar sign you actually want in the text.

Which flags does the pattern run with?

`g` and `u` are on by default. `g` finds every match instead of stopping at the first, and `u` makes the pattern read the text a character at a time, which is what makes `\p{L}`, `\p{N}` and friends mean what they say. Add `i` for case-insensitive matching, `m` to let anchors work per line, `s` to let a dot match a line break. Clearing the box runs with no flags at all. Any other letter — `x`, `y`, `d` — refuses the run rather than being ignored.

Is this the same as the find and replace page?

No, and the difference is what you type in the box. That page matches characters exactly as written, so the only thing that can go wrong is finding nothing. This page runs a program, which can match thousands of places in a single pass and rewrite all of them. That is why the pattern starts empty here, why unknown flags are refused, and why the two pages are separate: on one page a rewrite that did not happen is a typo, on the other it might be a pattern that quietly meant something else.

What happens when two matches overlap?

The leftmost one wins and the scan continues after it, which is what every regular expression engine does. So `aa` against `aaaa` finds two matches and not three, and after replacing them the text is `bb`. If you were expecting three, the pattern is describing something the engine cannot do, and it is better to know that before the file comes back.

Will the page protect me from a pattern that is slow?

It refuses one family of them before anything runs: a repetition applied to a group that already repeats without a bound, like `(a+)+` or `(\d+)+`. Those are the patterns that lock a browser tab, and thirty characters of input is enough. Three shapes are not caught and are worth avoiding by hand — two overlapping alternatives inside a repeating group, like `(a|a)+`; several overlapping repeats side by side, like `a*a*a*b`; and enormous counts, like `(a{100}){100}`. Nothing is checked against a clock, because a time limit would make the same file come out differently on a fast machine and a slow one.

What happens to italics and other styling when the text is rewritten?

A tag outside the edit shifts along with the text, and a tag that contains the edit stretches or shrinks to match. When a match crosses a tag's edge there is no correct place left for the markup, so it is dropped and the loss report says how many were dropped. Karaoke timing is dropped whenever a match touches it, because the per-syllable split no longer lines up with the words.

Can I use this on any subtitle format?

Yes. The pattern is applied to the text of each cue, and all twelve formats read that text out, so an SRT, a WebVTT file, an ASS file with styles, an LRC lyric sheet or a plain transcript all work the same way. Each file is written back in the format it came in, and a MicroDVD file's frame numbers use the frame rate in the panel.

Related tools