Working with voice tags with string.match

I have an xml page in which I have a monitoring system scan, here is the source data:

`<queues> <queue name="workQueue"> <stats size="0" consumerCount="28" enqueueCount="29320" dequeueCount="37000"/> 

And here is the code that I still have:

 local pattern = " size=(%d+) " local a = alarm.get("CO13974960-19518") local vsize = string.match(a.message, pattern) local sum = vsize 

I am trying to target this bit of data from an XML page:

 stats size="0" 

The value "0" is the number that interests me, and I'm looking for a way to capture this shape (no matter what it reaches) through the script.

I think my script is looking for:

size=0 , not size="0"

But I'm not sure of the correct syntax for how to do this.

+6
source share
1 answer

In general, it is not recommended to use the Lua pattern (or regular expression) for parsing XML, instead use the XML parser.


Anyway, in this example

 local pattern = " size=(%d+) " 
  • The space matters, so the space at the beginning and the end trying to match the space character, but failed.
  • You already noticed that you need double quotes around (%d) , they must be escaped in double quotes.
  • + greedy, he can work here, but not greedy - better choice.

It works

 local pattern = "size=\"(%d-)\"" 

Note that you can use single quotes so that you do not need double quotes:

 local pattern = 'size="(%d-)"' 
+5
source

All Articles