-1

I would like to capture a part of the response I am receiving as part of webservice call using regex. Here is the response I am receiving and I am interested to capture the status of just ContactMessageTransport queue.

Status of the queue is being sent in the line:

<pogo:Status>Started</pogo:Status> 

and queue name in the line:

<pogo:Name>ContactMessageTransport</pogo:Name> 

I used <pogo:Name>ContactMessageTransport[\w\W]*Started<\/pogo:Status> and its getting the status of other queue as well which I am not interested. I am finding it hard to match just the few lines. Can you please help?


 <getAllMessageQueueInfoResponse xmlns="http:abcd.com/MessageQueueAnalyticsAPI"> <return> <Entry xmlns:pogo="http://example.com/com/integration/services/messagequeueanalyticsservice"> <pogo:AckCount>0</pogo:AckCount> <pogo:DestinationID>0</pogo:DestinationID> <pogo:ErrorCount>25</pogo:ErrorCount> <pogo:ID>67</pogo:ID> <pogo:Latest>2017-11-28T00:00:00-05:00</pogo:Latest> <pogo:Name>ContactMessageTransport</pogo:Name> <pogo:NotAckCount>0</pogo:NotAckCount> <pogo:Oldest>2017-11-28T00:00:00-05:00</pogo:Oldest> <pogo:RetryableErrorCount>31</pogo:RetryableErrorCount> <pogo:SkippedCount>0</pogo:SkippedCount> <pogo:Status>Started</pogo:Status> <pogo:UnsentCount>212</pogo:UnsentCount> </Entry> <Entry xmlns:pogo="http://example.com/com/integration/services/messagequeueanalyticsservice"> <pogo:AckCount>0</pogo:AckCount> <pogo:DestinationID>0</pogo:DestinationID> <pogo:ErrorCount>0</pogo:ErrorCount> <pogo:ID>65</pogo:ID> <pogo:Latest>2018-03-17T00:00:00-04:00</pogo:Latest> <pogo:Name>Email</pogo:Name> <pogo:NotAckCount>0</pogo:NotAckCount> <pogo:Oldest>2018-03-17T00:00:00-04:00</pogo:Oldest> <pogo:RetryableErrorCount>4</pogo:RetryableErrorCount> <pogo:SkippedCount>0</pogo:SkippedCount> <pogo:Status>Started</pogo:Status> <pogo:UnsentCount>0</pogo:UnsentCount> </Entry> 

4
  • You'll be better off using an XML parser then trying to regex this. That being said if you must use a regex, what other language are you using here, python? Commented Jun 11, 2018 at 15:01
  • Regex is my only option. Its java Commented Jun 11, 2018 at 15:03
  • I can help with the regex and could get a simple example in python going manipulating the text, but I don't know Java well enough. If that is helpful let me know and I'll write up an answer. Commented Jun 11, 2018 at 15:08
  • Thank you sniperd. Appreciate your help Commented Jun 11, 2018 at 16:49

1 Answer 1

0

Is it possible that you are missing a simple ? lazy token? It can be used on a quantifier to only match the shortest possible sequence.

ContactMessageTransport[\w\W]*?Started<\/pogo:Status>

EDIT: Assuming there is always 8 lines to match:

ContactMessageTransport([^\r\n]*[\r\n]){8}

EDIT 2:

ContactMessageTransport[\s\S]*?Started(?:[^\r\n]*[\r\n]){3}

  • [\s\S]*? matches any character, up to Started.
  • (?:[^\r\n]*[\r\n]){3} matches 3 lines after Started. The added ?: is only used to prevent the parentheses from creating a group, which isn't needed. (It's called a "Non-capturing group")
Sign up to request clarification or add additional context in comments.

10 Comments

Nice. That seems to have did the trick. I would like to enhance this a bit more. If I have to limit the regex to check the next two lines after the word Started? I am trying to see if I can match from the name that contains the queue line and till </Entry>. This way I get to validate the response for one queue completely.
Just replace the <\/pogo:Status> part with <\/Entry>
Thanks Nicolas. Sorry for not being clear. I would like to achieve the same result using an alternative way using multiline check like this one stackoverflow.com/questions/37687883/… using { and } characters.
See my edit if it works.
Thanks again. This works as expected. For my understanding I am asking this question. \r matches the carriage return character and \n matches the linefeed or newline character, is ^ that is used for multi-line matching? I assume the number 8 is indicating the lines the regex to consider. Any tips from you would help my understanding. Thank you again.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.