-1

I have a huge XML document I want to replace double quotes (") with its html entity " inside double quotes, I think it can be done using regular expressions in Sublime Text but I don't know what can be the regular expression for that!

XML example:

<itemDescription descriptionValue="Rectangular Cocktail Table" itemDescriptionQualifier="SellerAssigned" itemDescriptionClassification="Product" itemFriendlyDescription="Rectangular Cocktail Table" itemFeatures="Welded metal frames in a baked epoxy "gunmetal" color finish." size="3.7""/>

what I want to achieve is:

<itemDescription descriptionValue="Rectangular Cocktail Table" itemDescriptionQualifier="SellerAssigned" itemDescriptionClassification="Product" itemFriendlyDescription="Rectangular Cocktail Table" itemFeatures="Welded metal frames in a baked epoxy &quot;gunmetal&quot; color finish." size="3.7&quot;"/>

Thanks

Muhammad
  • 6,725
  • 5
  • 47
  • 54

2 Answers2

2

It's better not to describe this as an XML document, or you will get the standard answer that you shouldn't process XML using regular expressions, you should use an XML parser. However, the reason you are doing this exercise is that this is NOT an XML document, and you want to turn it into one.

This isn't going to be easy. Your proposal of replacing " if it occurs between double quotes is clearly unworkable: which quotes would you replace in the text

abc="foo="bar" baz=" and="or""

There is simply no unique way of interpreting this, which is why XML has this rule in the first place.

You've been given a pig in a poke. Don't accept it. It's garbage. Tell the originator to fix it. XML has grammar rules for a good reason.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
-1

Type ctrl+h to "replace", On the left side of the text box, there are few buttons, you have to click the "regular expression" button (its like a dot and asterisk, or something),

Then, use the 2 text boxes to search (find, with your regexp) and replace!

Find your quote :

\"

Replace :

&quot;

Thats it!

Julo0sS
  • 2,096
  • 5
  • 28
  • 52
  • But I am looking for the regular expression, I know where to apply the regex. – Muhammad Mar 28 '17 at 11:45
  • @Muhammad Okay, here it is ! – Julo0sS Mar 28 '17 at 11:49
  • Oh Man, I can find the `"` but I want to find all `"` that are inside `"` and `"`. – Muhammad Mar 28 '17 at 11:53
  • Okay, did you look on stack / google? http://stackoverflow.com/questions/12290882/regex-to-escape-double-quotes-inside-double-quotes-with-preg-replace – Julo0sS Mar 28 '17 at 12:01
  • The example in the link shows you how to do it with few manipulations, First, extract the content between "real" quotes, and replace the quotes they contain with whatever you want.... That should do the trick – Julo0sS Mar 28 '17 at 12:18
  • http://stackoverflow.com/questions/28408865/replace-quotes-inside-quoted-string-with-escaped-quotes-in-notepad – Julo0sS Mar 28 '17 at 12:20
  • I tried that but that's not working, it is also including the outer quotes and if there is double quotes again inside it will not work. – Muhammad Mar 28 '17 at 12:21
  • The regexp the guy shows in his example is working fine to get the whole expressions you have in double quotes in your xml ("[^"]*)("\w*)(")([^"]*") – Julo0sS Mar 28 '17 at 12:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139246/discussion-between-julo0ss-and-muhammad). – Julo0sS Mar 28 '17 at 12:27