4 February 2024

To match an SSH public key using regular expressions (regex), you can use the following pattern. SSH public keys typically start with "ssh-rsa" or another algorithm identifier, followed by the key data.


  • ^: Start of the string.
  • ssh-[a-z]+: Match the algorithm identifier, such as "ssh-rsa".
  • \s+: Match one or more whitespace characters.
  • [A-Za-z0-9+/=]+: Match the key data, which is base64-encoded.
  • \s: Match a single whitespace character.
  • [^\s]+: Match the comment or label part of the key, which should not contain whitespace.
  • $: End of the string.
Source code viewer
  1. ^ssh-[a-z]+\s+[A-Za-z0-9+/=]+\s[^\s]+$
Programming Language: PCRE