Skip to content

Instantly share code, notes, and snippets.

@AlexanderAllen
Last active December 29, 2023 10:49
Show Gist options
  • Save AlexanderAllen/63a742d00b6ac15be53916c929f913ce to your computer and use it in GitHub Desktop.
Save AlexanderAllen/63a742d00b6ac15be53916c929f913ce to your computer and use it in GitHub Desktop.
GPG Identity Regex

PCRE 2+

(?x)

# fingerprint and grip records only have about 12 fields,
# so the match can't be too strict with the number of mandatory fields.

^
(?<FullRecordType>
(?<fieldRecortdType>[^:]*):
# 2
(?<fieldValidity>[^:]*):
# 3
(?<fieldKeyLength>[^:]*):
# 4
(?<fieldAlgo>[^:]*):
# 5 - field id
(?>[^:]*):
# 6
(?<fieldDate>[^:]*):
# 7
(?<fieldExpires>[^:]*):
# 8
(?<fieldSerialHash>[^:]*):
# 9 - owner trust
(?>[^:]*):
# 10 - IMPORTANT - USER ID !!!
(?<fieldUserID>[^:]*):
# 11 - sig class
(?>[^:]*):
# 12 - identity keys have no capability
(?<fieldCapabilities>[^:]*):

# 13 - issuer other info
# 14 - flag field
# 15 - serial number
(?>[^:]*):{3}

# 16 - hash algo
(?<fieldHashAlgo>[^:]*):
# 17 - curve name
(?<fieldCurveName>[^:]*):

# 18 - compliance
# 19 - udpated
# 20 - origin
# 21 - comment
(?>[:\d]){3,4}

# End MainRecordType.
)


# Short Record Type.
# (?<ShortRecordType>)*


@AlexanderAllen
Copy link
Author

AlexanderAllen commented Dec 29, 2023

Final: https://regex101.com/r/Ig8Xpc/1

Semi-uglyfied (line breaks):

(?x) (?<KeyRecordType>(?<fieldKeyType>pub|sub):(?<fieldKeyStatus>[^:]*):(?<fieldLength>[^:]*):(?<fieldPubKeyAlgo>[^:]*):(?<fieldKeyID>[^:]*):(?<fieldCreated>[^:]*):(?<fieldExpires>[^:]*):(?<fieldTrust>[^:]*):(?<fieldOwnerTrust>[^:]*):(?<fieldUserID>[^:]*):(?<fieldSigClass>[^:]*):(?<fieldCapability>[escaD?]+)\w*:(?:[^:]*:){4}(?<fieldCurveName>[^:]*):(?<fieldRest>[:\d]*)\n?) (?<FingerprintRecordType>^(?:fpr|fp2):(?:[^:]*:){8}(?<fingerprint>\w*):(?:[^:]*:)*?\n?) (?<GripRecordType>^grp:(?:[^:]*:){8}(?<grip>\w*):(?:[^:]*:)*?\n?) (?<IdentityRecordType>uid:(?=u)(?<fieldIdentityStatus>[^:]):(?:[^:]*):{3}(?<fieldIdentityCreated>[^:]*)(?:[^:]*):{2}(?<fieldIdentityID>[^:]*)(?:[^:]*):{2}(?<fieldIdentityComment>[^:]*):(?<fieldIdentityRest>[:\d]*)\n?)*

@AlexanderAllen
Copy link
Author

Remember to use with gpg --fingerprint --fingerprint --with-keygrip --with-colon.

Final, uglified (no spaces), tested on JS mode: https://regex101.com/r/8Ul2G2/1

(?<KeyRecordType>(?<fieldKeyType>pub|sub):(?<fieldKeyStatus>[^:]*):(?<fieldLength>[^:]*):(?<fieldPubKeyAlgo>[^:]*):(?<fieldKeyID>[^:]*):(?<fieldCreated>[^:]*):(?<fieldExpires>[^:]*):(?<fieldTrust>[^:]*):(?<fieldOwnerTrust>[^:]*):(?<fieldUserID>[^:]*):(?<fieldSigClass>[^:]*):(?<fieldCapability>[escaD?]+)\w*:(?:[^:]*:){4}(?<fieldCurveName>[^:]*):(?<fieldRest>[:\d]*)\n?)(?<FingerprintRecordType>^(?:fpr|fp2):(?:[^:]*:){8}(?<fingerprint>\w*):(?:[^:]*:)*?\n?)(?<GripRecordType>^grp:(?:[^:]*:){8}(?<grip>\w*):(?:[^:]*:)*?\n?)(?<IdentityRecordType>uid:(?=u)(?<fieldIdentityStatus>[^:]):(?:[^:]*):{3}(?<fieldIdentityCreated>[^:]*)(?:[^:]*):{2}(?<fieldIdentityID>[^:]*)(?:[^:]*):{2}(?<fieldIdentityComment>[^:]*):(?<fieldIdentityRest>[:\d]*)\n?)*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment