How to use RegEx in WHERE

Hello again :slight_smile:

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

{{ $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 on the goHugo Page for findRe (findRE | Hugo). Where I can see that the expression is

`^\d` gm

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

Warm Regards,
Sid

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

1 Like

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.

1 Like

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.

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.

Yes.

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