How to clean the details with regex?

Non-capturing/non-matching regular expressions (regex)?

  • I'm trying to use a regex expression to get details from within XML tags without matching the tags themselves. For example: <tag>Hello there</tag> would return: "Hello there" I've tried using the ?: operator like so: (?:<tag>).*(?:</tag>) which doesn't work. Perhaps I've misunderstood ?:'s function. Either way, I'd like to see a regex expression to achieve my desired result. Thanks for your time, Derek

  • Answer:

    You can fix your statement above by simply removing the greedy operators (?:) all together. However, I think you will find that regular expressions are not very good at doing what you are trying to do. They don't match nested bracket pairs (or in your case, pairs of tags) in the correct way whether you use the greedy operator or not. For example, if you have something like this: <tag><tag><tag>Hello there</tag></tag></tag> You won't be able to match the start/end tags. If you are doing this in perl, there are plenty of xml parsers out there you can use. Writing your own is hard and won't work the way you are trying to do it.

Derek at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.