Regular Expressions - match content in XML page

475 views Asked by At

I am new to regular expressions and need to write one that will pull certain data out of an XML page. For instance,

<name>Number of test runs</name>
<value>2</value>

The only number I need to pull is the 2. I want it to look at the XML tag Name so I don't pull from any other numbers on the page. Below is what I have but I am matching all the content instead of just the 2. Any help would be appreciative.

Current Regular Expression:

/<name>Number of Failed BGPs</name>\n<value>(.+?)/
1

There are 1 answers

3
Brian Stephens On

You said the problem is that it's matching all the content, not just the value (2). But you do need to match all the content to ensure it's the correct <name> tag.

The distinction you want is the matched group, designated by parens.

/<name>Number of Failed BGPs<\/name>\n<value>(.+?)<\/value>/

You want to get the first matched group, which should be just the value itself. Notice I also added the </value> tag to the regex. If you don't, your lazy quantifier would pick up only the first digit.