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

Questions about XML that are not covered by the other forums should go here.
victorv
Posts: 1
Joined: Sun Apr 04, 2021 9:22 pm

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

Post 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.
adrian
Posts: 2860
Joined: Tue May 17, 2005 4:01 pm

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

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply