Chyper64
Chyper64

Reputation: 113

Insert elements (strings) within existing XML tags using a RegEX?

Insert attributes (strings) within existing tags.

I need to insert the number found within <pagenum><pagenum/> into the id=attribute so in this example.

I start with:

<pagenum page="normal" id="page">1<pagenum/>

and I need to replace it with:

<pagenum page="normal" id="page1">1<pagenum/>

The string in there could be any alphanumeric value.

What would this regex look like? Something like this?

/s/<pagenum page="normal" id="page">1<pagenum//>/<pagenum page="normal" id="page"{Value}>1\<pagenum/>

I`m too rusty on my backreferencing...

Upvotes: 1

Views: 297

Answers (1)

mathematical.coffee
mathematical.coffee

Reputation: 56915

  • I changed the regex delimiter from / to ! to have a little less confusion)
  • to escape characters you use a backslash not a forward slash

Try:

s!(<pagenum page="normal" id=")([a-z0-9_-]+)(">)([0-9]+)(<pagenum/>)!\1\2\4\3\4\5!i

e.g.:

echo '<pagenum page="normal" id="page">1<pagenum/>' | \
sed -r 's!(<pagenum page="normal" id=")([a-z0-9_-]+)(">)([0-9]+)(<pagenum/>)!\1\2\4\3\4\5!i'

Note - isn't a closing tag usually </pagenum> as opposed to <pagenum/>?

Upvotes: 2

Related Questions