Page 1 of 1

Find and wrap all instances of a certain regex search at once

Posted: Sun Apr 04, 2021 9:41 pm
by victorv
I have a document which I need to find and wrap all instances inside brackets with a tag and keep the content inside the brackets. For instances. I have [1], [13, 14], [22] etc. in my document and I need to find using regex which I use \[.*?\] and then replace with
[<xref ref-type="aff" rid="R1">1</xref>]
[<xref ref-type="aff" rid="R13">13</xref>, <xref ref-type="aff" rid="R14">14</xref>]
etc.

Is there a way to do this all at once. Essentially finding and keeping the numbers I find and replace with a tag and use that number inside the tag.

Re: Find and wrap all instances of a certain regex search at once

Posted: Mon Apr 05, 2021 3:23 pm
by adrian
Hi,

You can use capturing groups . A pair of parenthesis in a regular expression defines a capturing group. Then you can refer it with $i.
$i - Match of the capturing group i.
* $i is the string that has been saved as capturing group i (i is a number).
* $0 is the subsequence matched by the entire expression.
Find: \[(.*?)\]
Replace: [<xref ref-type="aff" rid="R$1">$1</xref>]
$1 refers to whatever matches (.*?) between the square brackets. As you can see, you can also refer it multiple times.

Regards,
Adrian