Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
Python

r"
\A(?:(?P<type>[^\(!:]+)(?:\((?P<scope>[^\)]+)\))?(?P<breaking>!)?: (?P<description>.+))(?:(?!(?:\n{2}^(?:BREAKING[ -]CHANGE|[a-zA-Z0-9_\-]+)(?:: | #\b)[^\n:]+(\n*\b)?)+)\n{2}(?P<body>(?:(?!\n{2}(?:^(?:BREAKING[ -]CHANGE|[a-zA-Z0-9_\-]+)(?:: | #\b)[^\n:]+(\n*\b)?)+).|\n+?)+?))?(?:\n{2}(?P<footers>(?:^(?:BREAKING[ -]CHANGE|[a-zA-Z0-9_\-]+)(?:: | #\b)[^\n:]+(\n*\b)?)+))?(?:\n*)\Z
"
m

Description

This RegEx can be used to check whether a string (usually a commit message) matches the structure specified in the conventional commits specification and it allows to retrieve the sections of the structure via. named groups.

The groups are the following: (? means, that this can be empty/nullable)

  • type - The commit type (e.g. feat, fix, etc.)
  • scope? - The optional scope of the commit (e.g. the part of the codebase that the changes affect)
  • breaking? - ! or empty - whether breaking changes where marked using an exclamation mark after the type/scope
  • description - The (short) one-line description of the commit
  • body? - The optional (multiline) long description of the commit
  • footers? - The optional footer(s) of the commit

If you have any questions or found something that is wrong take a look on my GitHub for contact details

Submitted by mccoderpy - 2 years ago