Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 16180

Regex case-insensitive search, but have output match the case in the regex

$
0
0

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

Sample words list

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.


Viewing all articles
Browse latest Browse all 16180

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>