0

Good afternoon all,

I've noticed they're a few similar problems on here already but non have been of much help to me so far. I am trying to manipulate a String returned by and existing method to format it in to a readable form. So for example I am trying to creates a String replaceAll() regular expression which will take the string

"<ds:AddressLine1>Birkmire Farm</ds:AddressLine1><ds:AddressLine2>Some Village</ds:AddressLine2><ds:AddressLine3>Wigfield</ds:AddressLine3><ds:AddressLine4>Cumbria</ds:AddressLine4><ds:UKpostcode>CA9 1EJ</ds:UKpostcode>"

and convert it to...

Birkmire Farm, Some Village, Wigfield, Cumbria, CA9 1EJ

The way I envisaged doing this was in three steps, firstly to replace all the closing tags with "", then to replace all the opening tags with ", " and finally use the String replaceFirst to remove the first comma and space placed at the front of the string.

The issue I am having is the RegEx I need to formulate a pattern to identify any opening tag ie and a pattern to identify any closing tag ie . Any help on this would be greatly appreciated.

JonnyIrving
  • 753
  • 2
  • 8
  • 18
  • 2
    Try not to mix regex with a non-regular language. – AlanFoster May 14 '12 at 17:02
  • 5
    regex is not a right tool to parse xml. use xml parser – Prince John Wesley May 14 '12 at 17:03
  • 1
    Have you forgotten [what happens when you try to parse XML/HTML with regex?](http://stackoverflow.com/a/1732454/399649) – Justin Morgan - On strike May 14 '12 at 19:48
  • Okay, all points above understood, and the posted link article makes it very clear :). But it is not an actual xml document I am parsing it's merely the string I gave in my question. Prince John, thank you for your suggestion I'm looking into that now. Any further suggestions on the best method of doing this would be appreciated if I am indeed barking up the wrong tree with my method. – JonnyIrving May 15 '12 at 08:27

1 Answers1

1

RE for opening tag: <[^/][^>]*>. RE for closing tag: </[^>]*>.

Rob I
  • 5,627
  • 2
  • 21
  • 28
  • Thanks Rob, this has done the trick! Although all the talk of xml regex resulting in ever lasting pain has got me a bit worried! – JonnyIrving May 15 '12 at 08:37
  • Definitely good to be worried. Honestly Java SAX parsing is super easy so I would check it out. [Here](http://stackoverflow.com/questions/6495720/question-regarding-xml-parsing-using-sax-in-java) is a SO question with an example; there's a few other useful links in there... – Rob I May 15 '12 at 13:04