Search code examples
regexgrafanagrafana-variable

Match with capturing groups inside of lookaheads


I have following texts:

elasticsearch_sync_queue_lowerlevelgiberish_ManualAllocationPolicy:pending
elasticsearch_sync_queue_SynchronizationProcessor:processing

I'd like to extract text and value like:

text=ManualAllocationPolicy 
value=elasticsearch_sync_queue_lowerlevelgiberish_ManualAllocationPolicy
text=SynchronizationProcessor
value=elasticsearch_sync_queue_SynchronizationProcessor

I've used this expression but I'm not able to remove the text :*.

^(?=.*?(?<text>[A-Z][^:]*))(?<value>[^:]+)

https://regex101.com/r/lGhrPu/1

EDIT: This is being used in a Grafana variable, so the result of query might be little bit different.


Solution

  • Your capturing groups are working fine. You should either be using the List function on Regex101, or complete the expression to include the missing parts by adding :.*$ at the end:

    ^(?=.*?(?<text>[A-Z][^:]*))(?<value>[^:]+):.*$
    

    Substitution is used to replace in a string, leaving the unmatching part in the output.