0

I'm trying to manually add some prefixes to my xml string, for this I need a particular java expression that I just can't seem to find, basically this my string:

 <?xml version="1.0" encoding="UTF-8"?>
<mediabericht xmlns:mb="http://www.mediabericht.org"><inhoud>Randominhoud 22</inhoud><netwerk>Twitter</netwerk><datum>1408625886036</datum><film>Gladiator</film></mediabericht>

I want to add prefixes to my elements, so it should look like this:

<?xml version="1.0" encoding="UTF-8"?>
    <mb:mediabericht xmlns:mb="http://www.mediabericht.org"><mb:inhoud>Randominhoud 22</mb:inhoud><mb:netwerk>Twitter</mb:netwerk><mb:datum>1408625886036</mb:datum><mb:film>Gladiator</mb:film></mb:mediabericht>

I want to do a few replaceall's but so far I had no luck

The first regex I need to solve is < but not <? or </ and use the replaceAll function to replace these by <mb: (the prefix)

Then the second replacement I've handled:

String prefixedString = xmlString.replaceAll("</", "</mb:");
Robin-Hoodie
  • 4,886
  • 4
  • 30
  • 63

3 Answers3

1

The first regex I need to solve is < but not <? or </ and use the replaceAll function to replace these by <mb: (the prefix)

This can be solved using look-ahead:

String prefixedString = xmlString.replaceAll("<(?![/?])", "<mb:");
Keppil
  • 45,603
  • 8
  • 97
  • 119
1

You can try with Lookaround

The Lookaround (zero length assertions) actually matches characters, but then gives up the match, returning only the result: match or no match.

<(?!\/|\?)

Replacement : <mb:

OR

(?<=<)(?!\/|\?)

Replacement : mb:

Online demo

Patten explanation:

  (?<=                     look behind to see if there is:
    <                        '<'
  )                        end of look-behind
  (?!                      look ahead to see if there is not:
    \/                       '/'
   |                        OR
    \?                       '?'
  )                        end of look-ahead

Sample code:

System.out.println("<abc></abc>".replaceAll("(?<=<)(?!\\/|\\?)", "mb:"));  

OR simply use <\b as suggested by @Avinash Raj in below comments where \b is used as word boundary.

System.out.println("<abc></abc>".replaceAll("<\\b", "<mb:")); 
Braj
  • 46,415
  • 5
  • 60
  • 76
1

You can do everything with only one regexp:

String prefixedString = xmlString.replaceAll("(</?)([^?])", "$1mb:$2");

will correctly replace the opening and closing tags (and ignore the <?xml> tag) in only one replaceAll() call.

Florent Bayle
  • 11,520
  • 4
  • 34
  • 47