Search and replace using regex and xpath

Oxygen general issues.
llpick
Posts: 13
Joined: Sat Jun 23, 2018 12:52 am

Search and replace using regex and xpath

Post by llpick »

Hi there,

I need to add a bold tag (b) to callout numbers that have the form (1), (2), (n).
The regex would be something like:

Code: Select all

\(\d+\)
The tag has already been applied without consistency, so I'm trying to finish the job so to speak. But I don't want to apply the tag if it's already there.
Since Oxygen can do a search and replace using Xpath, I looked at how to restrict the search and replace to callouts that don't already have the tag. It would look something like:

Code: Select all

[not(ancestor::b)]
I'm kind of missing the next step: how to actually perform the search and replace operation?

Thank you for your help.
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: Search and replace using regex and xpath

Post by adrian »

Hi,

First, [not(ancestor::b)] will not work, because XPath actually comes first (it defines the searched regions) and you have at least one parent element that wraps your text (so it includes both text and b tag) and that one doesn't have the b ancestor. So XPath will match that parent element and still find the text wrapped in b tag within that region.
Instead, search for:

Code: Select all

(?<!<b>)\(\d+\)
(this means it shouldn't be preceded by <b> tag).
replace with:

Code: Select all

<b>$0</b>
and leave XPath empty (or filter by the parent element, if you need it to be more precise).
This works with both Find/Replace and Find/Replace in Files.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
llpick
Posts: 13
Joined: Sat Jun 23, 2018 12:52 am

Re: Search and replace using regex and xpath

Post by llpick »

Thank you so much Adrian, it works fine (and with a 100% regex answer to boot)!
Post Reply