Regex function/syntax issues (one more)

Why is the output of this snippet empty ? There is no error message.
I checked the regexps themselves as much as I could so it may be the syntax and sorry excuse of a regex engine.
I would appreciate very much if someone could see the error in this.

{{ $author := .Attributes.author  }}
{{ $cite   := .Attributes.cite    }}
{{ $class   := .Attributes.class }}

{{ if or (strings.Contains $cite "www") (strings.Contains $cite "http") }}
	{{ $text := ""}}
	{{ $url := ""}}
	{{ if in "$cite" "](" }}
		{{ $text := index (findRE `(?:\w+|\s|[^\[|\(|\]|\)])+` (index (findRE `\[(.*)]\(` $cite 1) 1)) 1 }}
	{{ end }}
	{{ $url = index (findRE `((https?://)|(www\.))[^)]*` $cite 1) 1 }}
	{{ warnf "text: %v url: %v" $text $url }}
	{{ .Page.Scratch.Add "Sources" (slice (dict "url" $url "Text" $text)) }}
{{ end}}

Well, without knowing the input, who would know? My crystal ball is out for repair.

Aside:

While you’re certainly entitled to your opinion, does this evaluation of the RE engine help in any way to explore the (non-explained) problem you are feeling to have?

Then, you should make it easier to help you. Why don’t you say

  • what you want to achieve?
  • what the input to your REs look like
  • what you’re expecting to see
  • what you are seeing instead
1 Like

Yes it’s ranty but sadly what else can I do ? you have a point though in that it comes from too higih above (gotemplate developpers ?) to change any time soon since they made their mind. Probably no one here could do anything even if the whole forum wanted, save for changing jobs…

the regex are supposed to extract links (with .Text) and all URLS in .Attribute.cite.
This is run through the entire site, so it should output many urls and many titles, and there are none. $text can be empty but never $url
Currently the only output is WARN 2023/01/20 13:41:22 text: url: <nil>
but I know the conditional below works well:

Check out regex101.com

I did check chem. and redid now. they work.

So, the regex is fine and the problem must be in your code. Since you ignore requests for details, I’m out of it.

1 Like

Sorry I thought I had given them.
It’s a code block renderhook with the language “quote”
See:
```quote{cite=“https://www.thecollector.com/pedophilia-ancient-greece-rome/”}
As mentioned
```
$url is supposed to become https://www.thecollector.com/pedophilia-ancient-greece-rome/
But if I have
```quote{cite=“[attention!](www.aol.fr)”}
essai
```
then $url becomes www.aol.fr and $text becomes attention!

$text will never be anything but empty because you do not change is value (read about the difference between := and =).

And you might want to check the working of index. Or maybe use first instead.

Aside: we have now two basic programming errors. Nothing at all to do with REs. Nor with their perceived shortcomings. I’d suggest more introspection and perhaps turning down the rants.

not having negative look-ahead or look-behind sucks or group extraction still sucks.
Also, if hugo chooses to do and say “nothing” when it meets two :=, couldn’t it complain instead ?Avoiding more basic mistakes.
From the site:

{{ $slice := slice "a" "b" "c" }}
{{ index $slice 1 }} => b

Isn’t it how I used it ? input = slice of strings with 1 element output = that one string

first 1 (findRE \[(.*)]\( $cite 1)

is still a slice of one string. Not a string. I can’t set $text or $url to a slice.
While the below should be a string.

index (findRE \[(.*)]\( $cite 1) 1

in Ada or related language it would look like

array := findRE(\[(.*)]\(, $cite, 1)
$url := array[1]

so yeah, syntax is something not to be happy about…

There’s no point in complaining about things you can’t change. Like Google’s RE implementation. Either live with it or move on to greener pastures. There are, afaik, SSGs out there that work with Javascript.

And the first index of a map is 0, not 1. As can be deduced from the code you quoted. first was actually a bad suggestion of me.

Pretty much my entire life philosophy. Complain until there’s a revolution, the culprits to correct things or commit suicide :sweat_smile:. Yes, javascript is so much worse, I will never understand how people can tolerate writing extensive in it. That or brainfuck, the difference is minimal imho.
This corrected code doesn’t seem to change anything :roll_eyes:
Honestly, is there no other way than index (findre (index (findre))) ?! That alone hurts my eyes…

{{ if in "$cite" "](" }}
	{{ $text = index (findRE `(?:\w+|\s|[^\[|\(|\]|\)])+` (index (findRE `\[(.*)]\(` $cite 1) 0) 1) 0 }}
{{ end }}
{{ $url = index (findRE `((https?://)|(www\.))[^)]*` $cite 1) 0 }}