I need help on regular expression in the below xml code I want to extract values in two tags (title,price) at a time so that my output should look like
Output required:
<title lang="en">Everyday Italian</title>
<price>30.00</price>
<title lang="en">XQuery Kick Start</title>
<price>29.99</price>
<title lang="en">XQuery Kick Start</title>
<price>49.99</price>
<title lang="en">Learning XML</title>
<price>39.95</price>
Right now I am using:
^\s*<title>.*</title>
this code is fetching only <title>
<title lang="en">Everyday Italian</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
How to get two tags at a time? can some one help me
XML:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Your regex wont match your given xml because you haven't handled attributes for title
tag. You can use this regex to get both title
and price
tags with a single expression:
^\s*<(title|price)[^>]*>(.*)<\/\1>
regex matching price tag example
same regex matching title tag example
Also you can get the tag-name and value using back-reference \1 and \2 to the captured groups.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments