How to get two or more matching tags in a xml file using regular expression

Suhas Bhatt

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> 
riteshtch

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to match two or more dots in a string using regular expression

How can I split the xml file using regular expression in Perl

Search and replace in only certain tags and attributes in XML file by a regular expression

How to trim any number of spaces in xml tags using a regular expression in vim?

Perl parse xml tags manually using regular expression

Regular expression to match non empty xml tags using Notepad++

Regular expression to replace text between XML tags using Notepad++

Two or more words in regular expression

Check the file name using pattern or regular expression matching in python

sed regular expression matching more than intended

using "or" in pattern matching regular expression

How to pass regular expression matching string from a file in awk?

how its better to filtering a file by using awk with more than two pattern matching?

How to match text and skip HTML tags using a regular expression?

how to get individual parameters using regular expression

How to handle xml file path given using regular expression to support multiple environment for Integration Testing by Spring?

How can I get all the contents between two pattern using python regular expression?

Matching one of two regular expression in javacscript

Is there a more computationally efficient way to find the first occurrence matching a regular expression using Pandas?

How to get the inner tags from an XML file?

Editing local XML file using Python and Regular expression

regular expression to search in xml file

How to find all the tags matching two values using BeautifulSoup in Python

How to get all the tags in an XML using python?

How to get a regular expression

Regular expression - File name not matching desired pattern

Regular expression for file name convention matching

python regular expression remove matching brackets file

Regular Expression - matching file extension from a URL

TOP Ranking

HotTag

Archive