Page 1 of 1

Search and replace using regex and xpath

Posted: Wed Jul 17, 2019 11:07 am
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.

Re: Search and replace using regex and xpath

Posted: Thu Jul 18, 2019 5:12 pm
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

Re: Search and replace using regex and xpath

Posted: Mon Jul 22, 2019 9:13 am
by llpick
Thank you so much Adrian, it works fine (and with a 100% regex answer to boot)!