Regex for Multi-Line Copyright Headers in C++ files

Our C++ source and header files need to have a copyright header, which we want to enforce with SonarQube. Rule cpp:S1451 “Track lack of copyright and license headers” appears to be perfect for this as you can specify a regular expression that the header should match. However I can’t figure out how to actually write a regular expression that will match our files.

Our copyright header looks like this:

////////////////////////////////////////////////////////////////////////////
//  Copyright Aperture Laboratories 2024
//  PortalUtils.cpp
//  Contains utility functions to work with portals.
////////////////////////////////////////////////////////////////////////////

The year is variable, and all lines except “Copyright” line are optional. My initial regular expression was:
///+\r?\n// Copyright Aperture Laboratories .+\r?\n(// .*\r?\n)*///+\r?\n.
This expression matches the header in Python or ripgrep, however SonarQube claims to use a different dialect it calls “POSIX regular expressions” and consequently the expression doesn’t match.

As the man page makes no reference to escapes for newlines (\r?\n) I tried literal newlines and a few other alterations to make the regex to conform to POSIX regular expressions.

We are using SonarQube Developer Edition Version 10.3 with the commerical C++ scanner.

Is it possible to match multi-line copyright headers in SonarQube?
How should our regular expression look like?

Hi @aperture and welcome on our Community.

I managed to match your copyright using the following regex: ^/+[[:space:]]+// Copyright Aperture Laboratories [[:digit:]]{4}[[:space:]]+//.*/$
I used the following page as a reference, which I found quite useful: POSIX Extended Regular Expression Syntax - 1.71.0

Thanks Fred, the proposed regex works for us.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.