Recently I rewrote my program to find words that are made of chemical symbol abbreviations, for example "HErSHeY". I came up with this dynamic regex:
grep -Pi "^($(paste -s -d'|' element_symbols.txt))+$" /usr/share/dict/words
The regex in the paste expands to something like (H|He|Li| ... )
. element_symbols.txt is a file starting with
HHeLi
The -i
makes the search case-insensitive, so "Hershey" is in the output, but is there a way to preserve the capitalizations of the letters within the regex, so the output is like "HErSHeY"? This would require replacing the letters in the words file, so maybe something with sed instead.