Since only the first 3 characters for the English-language mnemonic matter, I prefer to reduce the words to just the first 3 letters only, for cold storage purposes. I would like to do this with a regular expression, to avoid manual errors.

What would be the shortest regular expression to do this? Note that I want a newline at the end. I will post my current solution, looking forward to see a shorter one.

2 Answers 2


You can simply use (\w{3})\w* (example) or (?:^| )(\w{3}) (example), whatever you prefer. There are a lot of other solutions possible as well.

You didn't provide a language, hence my solution to generic regex outside of language implementations should be advantageous.

  • Indeed, I didn't specify a language. I especially like your 1st solution: echo "${MNEMONIC}" | ruby -e "puts ARGF.read.gsub(/(\w{3})\w*/,'\1')". Can't get the other one to work in Ruby though. Thanks also for the regex101.com examples.
    – dpzz
    Commented Nov 15, 2016 at 18:38

What I currently have:

echo $(echo "${MNEMONIC}" | tr " " "\n" | sed 's/^\(...\).*$/\1/' | tr "\n" " ")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.