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.

4
  • 2
    Try not to mix regex with a non-regular language. Commented May 14, 2012 at 17:02
  • 5
    regex is not a right tool to parse xml. use xml parser Commented May 14, 2012 at 17:03
  • 1
    Have you forgotten what happens when you try to parse XML/HTML with regex? Commented May 14, 2012 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. Commented May 15, 2012 at 8:27

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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!
Definitely good to be worried. Honestly Java SAX parsing is super easy so I would check it out. Here is a SO question with an example; there's a few other useful links in there...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.