Match specific length words anchored without doing magic math
Let's say I wanted to find all 12 letter words in /usr/share/dict/words
that started with c
and ended with er
. On top of my head, a working pattern might look something like this:
grep -E '^c.{9}er$' /usr/share/dict/words
He finds:
cabinetmaker
calcographer
calligrapher
campanologer
campylometer
...
But it worries me .{9}
. It feels too magical to subtract the total length of all anchor characters from the number defined in the original constraint.
Is there a way to rewrite this regex so that it doesn't require doing this calculation in front, while allowing the literal 12
to be used directly in the template?
source to share
I don't know grep
that well, but some more advanced NEX RegEx implementations provide you with views and lookbehinds. If you can find any ways to make them available to you, you can write:
^(?=c).{12}(?<=er)$
Perhaps as a perl
one-liner?
cat /usr/share/dict/words | perl -ne "print if m/^(?=c).{12}(?<=er)$/"
source to share