Regex invalid syntax

Hello,

I’m trying to remove from a string comment data with a regular expression, so I do

replaceRE "(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)" "" . 

But I get the error “invalid syntax” but I have tested the regular expression on

http://www.regexplanet.com/advanced/golang/index.html
https://regex-golang.appspot.com/assets/html/index.html

and both tools returns the correct result

See the following thread. You likely need to use proper escaping.

I have change the regex to

 (/\\*(?sU).*\\*/)|(//.*)

this works, but not with a shortcode parameter. I’m using a shortcode within the markdown file

{{< getfile regex="(/\\*(?sU).*\\*/)|(//.*)" >}}

and I pass it to replaceRE inside the shortcode:

{{ $mydata := // read data }}
<div>
{{ repaceRE (.Get "regex") "" $mydata }}
</div>

How can I pass the regex from markdown to replaceRE?

Thanks

That’s odd. Does it work if you hard-code it inside the shortcode?

Try double-escaping the parameter. It shouldn’t matter, but try it.

{{< getfile regex="(/\\\\*(?sU).*\\\\*/)|(//.*)" >}}

With a hard-coded version:

 (/\\*(?sU).*\\*/)|(//.*)

everything works fine, but with a parameter version it fails. If I do double-escaping the rehex will not match anymore.
If I put the regex (without double-escaping) on the shortcode parameter it will not match correctly, some parts are matched, some not.
My original shortcode is found here the three variables should be passed by a parameter.

First off, kudos on an awesome framework for including example code into your site.

The issue is that within the shortcode itself, the Go template engine is parsing the string, and it must be escaped.

When passing the regexp as a parameter to a shortcode, our shortcode parser handles that, and the parameters are treated a literal strings that don’t need to be escaped. So, use this:

{{< githubsource clear="(/\*(?sU).*\*/)|(//.*)" >}}

Thanks for your compliment. It will work with your solution, but I add also a default option for reducing tag complexity.
I will publish the whole source of the shortcode here to read any source code from a GitHub repository:

{{- $id := (.Get "id") -}}
{{- $codetype := .Get "lang" -}}
{{- $prefix := (.Get "prefix") | default "" -}}
{{- $postfix := (.Get "postfix") | default "" -}}
{{- $linenumbers := (.Get "linenumbers") | default "" -}}
{{- $branch := delimit (slice "?ref=" (.Get "branch" | default "master")) "" | string -}}

{{- $clear := .Get "clear" | default "(/\\*(?sU).*\\*/)|(//.*)" -}}
{{- $replace := .Get "replace" | default "(\n|\r)+" -}}
{{- $replacewith := .Get "replacewith" | default "\n" -}}

{{- $file := getJSON "https://api.github.com/repos/" (.Get "user") "/" (.Get "repo") "/contents/" (.Get "file") $branch -}}


{{- if .Get "filter" -}}

    {{- $result := findRE (.Get "filter") ($file.content | base64Decode) (.Get "result" | default 1 | int) -}}
    {{- range $result -}}
        <pre class="language-{{- $codetype }} {{ if gt (len $linenumbers) 0 -}}line-numbers{{- end -}}" {{ if $id }}id="source-        
{{- $id -}}"{{ end }} {{ if eq $codetype "agentspeak" -}}data-language="AgentSpeak(L++)"{{- end }}><code class="language-{{- $codetype -}}">
{{- replaceRE $replace $replacewith (delimit (slice $prefix "\n" (replaceRE $clear "" .) "\n" $postfix) "") -}}
        </code></pre>
    {{- end -}}

{{- else -}}

    <pre class="language-{{- $codetype }} {{ if gt (len $linenumbers) 0 -}}line-numbers{{- end -}}" {{ if $id }}id="source-{{- $id -}}"{{ end }} {{ if eq $codetype "agentspeak" -}}data-language="AgentSpeak(L++)"{{- end }}><code class="language-{{- $codetype -}}">
{{- replaceRE $replace $replacewith (delimit (slice $prefix "\n" (replaceRE $clear "" ($file.content | base64Decode)) "\n" $postfix) "") -}}
    </code></pre>

{{- end -}}

I’m using Prism.js for syntax highlighting and coloring with the line-number plugin (which is loaded inside the header partial). But keep in mind the GitHub API Limit for getting content during developing and disabled caching it will reached very fast.

Usage of the shortcut arguments:

  • id is the DOM ID of the code tag
  • codetype is the Prim.js language definition
  • prefix is an optional first line inside the code base e.g. an open XML tag
  • postfix similar to prefix but for the last line e.g. a closing XML tag
  • linenumbers enable line-numbers of the source code
  • branch GitHub repository branch which should be used (default is master)
  • user the GitHub username
  • repo the repository name of the user
  • file the path to the file within the repository
  • clear a regular expression which removed anything that matches e.g. document strings
  • replace and replacewith replaces an elements that matches the expression with the other element e.g. multiple empty lines
  • filter and result a regular expression to grep a set of lines from the source code for each match a new code element will be created and result will limit the number of elements (default 1)

In my case I’m using an own language definition “agentspeak” with special layout definition, so I have added an if-else condition for that.

Additional to the code shortcode, I create a line-link shortcode base on the Line Highlight plugin to reference / highlight source code lines:

{{- $link := delimit (slice (.Get 0) "#source-" (.Get 1) "." (replaceRE "[[:space:]]+" "" (.Get 2))) "" -}}
<a href="{{- $link -}}">{{- default (delimit (slice "line" (index .Params 2)) " ") (index .Params 3) -}}</a>
  1. parameter is the URL to the document (empty by default)
  2. is the DOM ID of the source code element
  3. are the line number for highlight
  4. optional a description text (default is “line #line numbers”)

Thanks for help :slightly_smiling: