Regular expression - how to match xml value

<>

This question already has an answer here:

Answers


In general, it's a bad idea to try parsing XML with regular expressions. The reason is that regex is insufficiently expressive, even with back references and such. The questions linked in the comments are worth reading to understand why this is generally a bad idea.

That said, you can be successful if you know for certain the format of your file, and if you're willing to do a little non-regex parsing as well.

In your situation, you have essentially:

<Flight>
    <AirlineCode>
    </AirlineCode>
<Flight>
<AirlineCode>
</AirlineCode>
<Flight>
    <AirlineCode>
    </AirlineCode>
<Flight>

And you want all of the <AirlineCode> tags that occur within <Flight> tags.

The way to approach this problem is to extract the <Flight> tags and their contents with one regex, and then use another regex to extract the <AirlineCode> tags from those extracted <Flight> tags. Don't try to do it in a single regular expression. You will not succeed.

If your data really is that simple, then this will work. I won't say that I recommend this approach. There are too many things that can go wrong. Data formats have a distressing tendency to change, and that fragile regex solution is likely to break if the format changes even a little bit. An XML parser solution will be much more robust.


Need Your Help

IE8 Clearing JQuery-UI Autocomplete Input Field After Selection

jquery-ui events internet-explorer-8 autocomplete focus

I've got a jquery_ui autocomplete. When I click on a selection in firefox my code runs as anticipated. When I run it in IE8, something is clearing the selection from the input box (after all my cod...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.