# How to use RegEx in WHERE

**URL:** <https://discourse.gohugo.io/t/how-to-use-regex-in-where/45310>\
**Category:** support\
**Created:** [July 16, 2023, 6:21pm UTC](https://discourse.gohugo.io/t/how-to-use-regex-in-where/45310 "2023-07-16T18:21:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sids](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@Sids](https://discourse.gohugo.io/u/Sids)\
**Post date:** [July 16, 2023, 6:21pm UTC](https://discourse.gohugo.io/t/how-to-use-regex-in-where/45310/1 "2023-07-16T18:21:55Z")

</div>

Hello again 🙂

I was reading about Regular Expressions in Hugo and found that we have a few functions like FindRe which would be helpful if you are trying to match a set of text with some pattern.

I tried to use a RegExp with WHERE to extract (print) the frontmatter from MD files where the Fruit ID that starts with a digit. But it did not print anything out. Must be something wrong with my RegEx

```auto
{{ $completeSet := (where $.Site.Pages ".Params.fruitID" `^\d` ) }}
        {{ range $completeSet }}
            {{.Params.ls_langID}}<br>
        {{ end }}

```

As it did not generate any build time errors, I assume that RegEx can be used in such WHERE instances. Just that I am doing it the wrong way.

Any tips?

Oh and also, I do find a mention of [regex101.com](http://regex101.com) on the goHugo Page for findRe ([findRE | Hugo](https://gohugo.io/functions/findre/)). Where I can see that the expression is

```auto
`^\d` gm

```

and the above would match any data starting with a digit. Should I be using that gm as well ?

Warm Regards,  
Sid

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 16, 2023, 6:36pm UTC](https://discourse.gohugo.io/t/how-to-use-regex-in-where/45310/2 "2023-07-16T18:36:52Z")

</div>

Unlike SQL, Hugo’s `where` function does not have a `LIKE` operator. This has come up before:

[https://discourse.gohugo.io/t/add-like-comparison-operator-to-the-where-function/42013/4](https://discourse.gohugo.io/t/add-like-comparison-operator-to-the-where-function/42013/4)

---

<div class="post-metadata">

**Author:** ![chrillek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chrillek/32/19797_2.png) [@chrillek](https://discourse.gohugo.io/u/chrillek)\
**Post date:** [July 16, 2023, 7:07pm UTC](https://discourse.gohugo.io/t/how-to-use-regex-in-where/45310/3 "2023-07-16T19:07:54Z")

</div>

> [@Sids](#):
>
> As it did not generate any build time errors, I assume that RegEx can be used in such WHERE instances

That’s not a logical conclusion: What you consider to be a regular expression is simply a string in this context. You’re asking for all pages with a `fruitID` parameter in their front matter that is literally “^\d”, i.e. caret + backslash + the letter d. There probably is no such page, so `where` returns nothing. But your code doesn’t contain a syntax error (only a semantic one), so Hugo has no reason to raise an error.

---

<div class="post-metadata">

**Author:** ![Sids](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@Sids](https://discourse.gohugo.io/u/Sids)\
**Post date:** [July 16, 2023, 7:18pm UTC](https://discourse.gohugo.io/t/how-to-use-regex-in-where/45310/4 "2023-07-16T19:18:39Z")

</div>

Oh. Okay. So the only probable way is -  
a. Either we loop through the results (as you mentioned on the 2nd reply to that discussion) or better still,  
b. use FindRE on that result (this should be the way to do it, I guess, that would not be correct/right way to do it - reason follows).

So now, the next obvious question -  
if I used FindRe, It would consider the entire data fetched in the WHERE as its input and quite likely there will be other fields that would start with a number/digit. How do I tell it (FindRe) to search only on one particular field?

Can we fetch just one field using WHERE ? (In my case, only the fruit\_ID) or I do it inside the loop as you said on the response?

Thank you.

---

<div class="post-metadata">

**Author:** ![Sids](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@Sids](https://discourse.gohugo.io/u/Sids)\
**Post date:** [July 16, 2023, 7:23pm UTC](https://discourse.gohugo.io/t/how-to-use-regex-in-where/45310/5 "2023-07-16T19:23:10Z")

</div>

Ah! Lame me (I blame it on the “Sunday” factor, I guess!).

100% correct.

I was of the belief that these caret symbols will have some special meaning to goHugo and reading your response, makes me realize that I was wrong.

As you say the entire `'string'` is interpreted as a string, quite rightly so. That is why I did not get any results - neither the error nor the page with any content.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 16, 2023, 8:13pm UTC](https://discourse.gohugo.io/t/how-to-use-regex-in-where/45310/6 "2023-07-16T20:13:26Z")

</div>

> [@Sids](#):
>
> or I do it inside the loop as you said on the response?

Yes.

1. Get a page collection
2. Range through the page collection, evaluating each page
