use RE to fix XML attributes

Find, replace, find in files, replace in files, regular expressions

use RE to fix XML attributes

Postby YourMum » Thu Nov 24, 2005 9:28 am

Hi folks,

I have an XML document, but it is not very well formatted: a large number of the node attributes are not enclosed in quotes. e.g.
Code: Select all
<Node attr1=hello attr2=goodbye>node text here</Node>


Is there a neat RE that I can run that will find all attributes that don't have a quote round their value and insert them?

Thanks for your assistance!!
User avatar
YourMum
Newbie
 
Posts: 4
Joined: Sun Sep 18, 2005 11:00 pm

Re: use RE to fix XML attributes

Postby Bego » Thu Nov 24, 2005 10:19 am

Hi Mum,

try this:
find string:
Code: Select all
\=([a-z]+)([\s>]*)

replace string:
Code: Select all
\='\1'\2


then it looks like this:
Code: Select all
<Node attr1='hello' attr2='goodbye'>node text here</Node>


NOTE: UNIX REGEXP IS ON !!! See "options, search"


rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: use RE to fix XML attributes

Postby YourMum » Thu Nov 24, 2005 12:41 pm

Hey Bego,

Thanks a million for this. It wasn't quite perfect for my situation, but gave me a huge head-start.

For the record, I used this:
find string:
Code: Select all
\=([a-z0-9]+)([\s>]*)

replace string:
Code: Select all
\="\1"\2
User avatar
YourMum
Newbie
 
Posts: 4
Joined: Sun Sep 18, 2005 11:00 pm

Re: use RE to fix XML attributes

Postby Bego » Thu Nov 24, 2005 7:04 pm

Hy mim,

thats what this forum is good for :D

Next time, also say what exactly the output should look like.

greetz from germany
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany


Return to Find/Replace/Regular Expressions