# Grok pattern with regex

**URL:** <https://discuss.elastic.co/t/grok-pattern-with-regex/101938>\
**Category:** Logstash\
**Created:** [September 27, 2017, 7:21am UTC](https://discuss.elastic.co/t/grok-pattern-with-regex/101938 "2017-09-27T07:21:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lois](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@lois](https://discuss.elastic.co/u/lois)\
**Post date:** [September 27, 2017, 7:21am UTC](https://discuss.elastic.co/t/grok-pattern-with-regex/101938/1 "2017-09-27T07:21:24Z")

</div>

Hi,

I am trying to match a string using a grok pattern with regex.  
An example string is:  
(1) **12.3 abc Chrome/4.56 OPR/7.89**  
In this case I want my regex to match **OPR**.  
In case the string looks like this:  
(2) **12.3 abc Chrome/4.56/7.89**  
I want my regex to match with **Chrome**.  
So, I want OPR to have precedence over Chrome.

If I simply use the regex (OPR) then it matches OPR.  
However, when I use the regex: **(OPR)|(Chrome)** it always matches Chrome (also in string (1)). I have also tested with other substrings (for example abc) in this regex and it seems to be matching the substring that occurs first in the entire string.

I read that in a regex the first match in the regex wins, so that would mean OPR would match. However, since this is not the case I am wondering what I am doing wrong. Can someone help me with this?  
Thank you in advance.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 25, 2017, 7:21am UTC](https://discuss.elastic.co/t/grok-pattern-with-regex/101938/2 "2017-10-25T07:21:47Z")

</div>

Closed on October 25, 2017, 7:21am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
