0

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>(.+?)/
Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • Which regex engine or language are you using? – Unihedron Aug 15 '14 at 16:39
  • To be honest I am not even sure. I am using in a monitoring tool within Sitescope. It only works with basic POSIX regex features – Nathan Wolford Aug 15 '14 at 16:43
  • Can you explain better, what you're going to achieve? Get `` this ``, if preceded by `Number of Failed BGPs\n` but don't get the part before including ``? Something [like this](http://regex101.com/r/xK4cT3/1)? – Jonny 5 Aug 15 '14 at 17:07
  • Number of test runs 2 – Nathan Wolford Aug 15 '14 at 17:22
  • These two statements will always appear together on the page. So I want to use the Number of test runs to make sure that I key in on the correct value. But I only want to match the digit in between the tags. – Nathan Wolford Aug 15 '14 at 17:24

1 Answers1

0

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.

Brian Stephens
  • 5,161
  • 19
  • 25
  • This is still not working as wanted. I am having a hard time on how to "key" of a phrase to get the next digit. Below is more of the XML page that I am trying to match the value for. You can see that the page has several '' tags. That is why I need to search for the Number of failed BGPs and then the next value after that. I want the Regular expression to only key on the value so I can monitor if that value is not = to 0. – Nathan Wolford Aug 20 '14 at 13:39
  • ' Number of Handled Requests 73.0 - Number of Running BGPs 0.0 - Number of Waiting BGPs 0.0 - Number Waiting In Data source -1.0 - Total Number of Waiting BGPs 73.0' – Nathan Wolford Aug 20 '14 at 13:41
  • I added your sample XML to the original question so it's easier for everyone to read. I escaped the slashes in my regex to fix that problem, but otherwise this should still be what you need. It's looking for "Number of Failed BGPs" specifically, and getting the value that follows immediately after. – Brian Stephens Aug 20 '14 at 14:02