Regular Expression for Adding an Alt Tag

<oXygen/> general issues.
jmorales
Posts: 15
Joined: Tue Oct 30, 2018 9:47 pm

Regular Expression for Adding an Alt Tag

Post by jmorales » Thu Jun 13, 2019 7:38 am

I'd like to use the Find/Replace in Files feature to add an <alt> tag to images that lack it. For example, change
<image deliveryTarget="pdf" href="../graphics/g_c_cda___cda_viewer_permissions_5_9_1.png" id="135356"/>
into
<image deliveryTarget="pdf" href="../graphics/g_c_cda___cda_viewer_permissions_5_9_1.png" id="135356"><alt/></image>

I think this could be done using the Regular Expressions option of Find/Replace, but implementations of Regular Expressions seem to vary a bit. I think I need something like named or numbered backreferences (https://www.regular-expressions.info/re ... ckref.html). Does Oxygen support capturing part of the text in the target and reusing it as part of the replacement value? I'm think of something like this:

Find
<image(capture everything here)/>
Replace
<image(previously captured text)><alt/></image>

Thanks for any suggestions!

adrian
Posts: 2531
Joined: Tue May 17, 2005 4:01 pm

Re: Regular Expression for Adding an Alt Tag

Post by adrian » Thu Jun 13, 2019 5:13 pm

Hi,

Oxygen supports Java regular expression syntax in the Find/Replace tools.

You can use $n to refer numbered capturing groups (where n is a number from 1-9). $0 refers the entire search match.

Search for:
<image (.*)/>
Replace with:
<image $1><alt/></image>

.* will capture anything, so you might want to use a more specific pattern

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

jmorales
Posts: 15
Joined: Tue Oct 30, 2018 9:47 pm

Re: Regular Expression for Adding an Alt Tag

Post by jmorales » Fri Jun 14, 2019 6:17 am

Thanks Adrian, that worked perfectly!

Post Reply