Extract templates from strings in Go.
Overview • How To Use • Installation • Contributions • License
Given some byte strings, this library extracts the common template between them -- some people call it "reverse templating". For example, given the following list of file names:
IMG_20180930_171704.jpg
IMG_20181001_150308.jpg
IMG_20181001_190338.jpg
IMG_20181021_122346.jpg
we can infer the common file name template: IMG_2018*_1*.jpg
.
Idea credits: turicas/templater.
There are two functions: "basic" and "advanced". Both return a slice of byte slices which correspond to template constant blocks. The former function is straightforward:
import "gopkg.in/vmarkovtsev/etalpmet.v1"
template := etalpmet.ReverseTemplate(
[]byte("<b> spam and eggs </b>"),
[]byte("<b> ham and spam </b>"),
[]byte("<b> white and black </b>"))
// ["<b>", "and", "</b>"]
The "advanced" function allows to change some parameters of the template extraction.
import "gopkg.in/vmarkovtsev/etalpmet.v1"
template := etalpmet.ReverseTemplateWithParameters(
5, // min block length
false, // trim
[]byte("<b> spam and eggs </b>"),
[]byte("<b> ham and spam </b>"),
[]byte("<b> white and black </b>"))
// [nil, " and ", " </b>"]
Note nil
which signals that there is text before the leftmost template block " and "
.
go get gopkg.in/vmarkovtsev/etalpmet.v1
The code supports building under Go >= 1.8.
...are pretty much welcome! See contributing.md and code_of_conduct.md.
Apache 2.0, see LICENSE. It allows you to use this code in proprietary projects.