# If field contains question mark

**URL:** <https://discuss.elastic.co/t/if-field-contains-question-mark/129608>\
**Category:** Logstash\
**Created:** [April 26, 2018, 8:23am UTC](https://discuss.elastic.co/t/if-field-contains-question-mark/129608 "2018-04-26T08:23:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KevSex](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevsex/32/29031_2.png) [@KevSex](https://discuss.elastic.co/u/KevSex)\
**Post date:** [April 26, 2018, 8:23am UTC](https://discuss.elastic.co/t/if-field-contains-question-mark/129608/1 "2018-04-26T08:23:22Z")

</div>

Hi,

I'm trying to filter out some URLs to split up the uri stem and query.

I've got the following pattern match which works when there is a questionmark to separate them both.

```
filter {
  grok {
    patterns_dir => "/etc/logstash/patterns"
    match => ["http_request", "%{GREEDYDATA:uri_stem}\?%{GREEDYDATA:uri_query}"]
   }
}

```

The problem I'm facing is that when the URL doesn't have any variables (and therefore no questionmark in the URL), I get a \_grokparsefailure.

I was trying to carry out an if statement to combat this as below:

```
if [http_request] =~ "?" {
  grok {
  ...
  }
}

```

However I'm still getting a parse failure.

Is there anything special that needs to be done to correctly identify a questionmark in a field.

p.s I've also tried "\\?" (double backslash) which doesn't work either

Any help would be appreciated.

Cheers,

---

<div class="post-metadata">

**Author:** ![KevSex](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevsex/32/29031_2.png) [@KevSex](https://discuss.elastic.co/u/KevSex)\
**Post date:** [April 26, 2018, 8:51am UTC](https://discuss.elastic.co/t/if-field-contains-question-mark/129608/2 "2018-04-26T08:51:04Z")

</div>

Turns out it only required one backslash

```
if [http_request] =~ "\?" {
 ...
}
```

---

<div class="post-metadata">

**Author:** ![Jenni](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenni/32/29684_2.png) [@Jenni](https://discuss.elastic.co/u/Jenni)\
**Post date:** [April 26, 2018, 8:51am UTC](https://discuss.elastic.co/t/if-field-contains-question-mark/129608/3 "2018-04-26T08:51:23Z")

</div>

Usually I would tell you to put `()?` around the optional part. But that doesn't seem to work with GREEDYDATA. So you'll probably have to add a second pattern in the array.

grok { match =\> { "http\_request" =\> ["%{GREEDYDATA:uri\_stem}?%{GREEDYDATA:uri\_query}", "%{GREEDYDATA:uri\_stem}"] } }

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 26, 2018, 9:00am UTC](https://discuss.elastic.co/t/if-field-contains-question-mark/129608/4 "2018-04-26T09:00:16Z")

</div>

You should really never use more than one `GREEDYDATA` pattern in any grok expression. Doing so is inefficient and error prone. Always try to use the most precise pattern possible, e.g. `WORD`, `NOTSPACE`, `NUMBER`, `IP` etc.

---

<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:** [May 24, 2018, 9:00am UTC](https://discuss.elastic.co/t/if-field-contains-question-mark/129608/5 "2018-05-24T09:00:20Z")

</div>

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