Skip to content
Advertisement

Regex make a group optional

I have the below messages to parse

Messages discarded due to Dispatch Queue cap: 0 / 60001 ms
Dispatch Queue size: 2
Dispatched Messages: 369 / 60001 ms
Dispatched message size: Average: 723, Entries: 39, Min: 366, Max: 1570
WinSockDispatcher: 53 / 8865 ms
UXDispatcher: 57 / 8865 ms

The regex which I have so far is

(Messages discarded.* Dispatch Queue cap|Dispatch Queue size|Dispatched Messages|WinSockDispatcher|UXDispatcher).+ (d.+).+ (d.+)

I am able to match almost everything I except the line

Dispatch Queue size: 2

Looks like the last group in my regex should be optional. I tried using ? but I am unable to figure out the proper syntax.

Can someone please suggest the change that would be help here?

Advertisement

Answer

This should give you the groups:

(Messages discarded.* Dispatch Queue cap|Dispatch Queue size|Dispatched Messages|WinSockDispatcher|UXDispatcher): (d+)(?: / +(d+.*))?
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement