Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace within a capture group

I am modifying an existing HTML doc. I'm doing things like adding a table of contents etc.

I have a heading with this ID: id="transcending intellectual limitations" (for real!)

I want to be able to find the whole ID, and then replace the spaces with hyphens.

It would be simple if I had just the IDs but I don't want to remove all the spaces in the whole document.

I'm reasonably new to regex, I'm using Sublime's find and replace to do this.

like image 925
Ben Avatar asked Feb 07 '23 22:02

Ben


1 Answers

You can use

(?:\bid="|(?!^)\G)[^\s"]*\K\s+ 

And replace with anything you need to replace spaces with.

The (?:\bid="|(?!^)\G) pattern sets the initial boundary: either id=" or the end of the last successful match. This pattern presents an alternation list with two alternatives. \b matches a word boundary so that id=" is matched as a whole word. The \G operator matches at the start of the string and after ech successful match. To exclude the start position, a negative (?!^) lookahead is added (not followed with a string start position). See more about \G in "Where You Left Off: The \G Assertion".

The [^\s"]* matches zero or more characters other than whitespace and a quote.

The \K operator makes the regex engine omit all the text matched so far from the match buffer.

The \s+ finally matches one or more whitespaces that will be replaced.

Regex101 Demo

like image 99
Wiktor Stribiżew Avatar answered Mar 02 '23 21:03

Wiktor Stribiżew