Remove all special characters and case from string in bash

cat yourfile.txt | tr -dc ‘[:alnum:]\n\r’ | tr ‘[:upper:]’ ‘[:lower:]’ The first tr deletes special characters. d means delete, c means complement (invert the character set). So, -dc means delete all characters except those specified. The \n and \r are included to preserve linux or windows style newlines, which I assume you want. The second …

Read more

What does ?: do in regex

It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s), even though it’s enclosed by () it won’t appear in the list of matches, only (\w+) will. You’re still looking for a specific pattern (in this case, a single whitespace character following at least one word), but you don’t …

Read more

Using named matches from Go regex

You can reference your named capture groups by utilizing map as follows: package main import ( “fmt” “regexp” ) var myExp = regexp.MustCompile(`(?P<first>\d+)\.(\d+).(?P<second>\d+)`) func main() { match := myExp.FindStringSubmatch(“1234.5678.9”) result := make(map[string]string) for i, name := range myExp.SubexpNames() { if i != 0 && name != “” { result[name] = match[i] } } fmt.Printf(“by name: …

Read more