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.
Find and wrap all instances of a certain regex search at once
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.
[<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
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.
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
You can use capturing groups . A pair of parenthesis in a regular expression defines a capturing group. Then you can refer it with $i.
Find: \[(.*?)\]$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.
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
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Return to “General XML Questions”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service