I am trying to use the semver.org recommended regex for tags:
Is there a suggested regular expression (RegEx) to check a SemVer string?
The one special condition my company requires is that the version tag begins with a v
… i.e. v0.1.0
.
Here is my config:
tags:
# SEE: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
# SEE: https://regex101.com/r/Ly7O1x/592
# This is that ^^ regex, except it begins with a literal v
only: ^v(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$
Which doesn’t currently work :/.
I think it’s because I need to add a leading/trailing /
. Upon doing so, and entering circleci config validate
, I get a config error:
Error: Unknown inline modifier near index 4
^v(?P<major
Can someone point out what I am doing wrong, or a way I can make this work?
Does CircleCI parsing regex allow named capturing groups, aka ?P
?
It depends on how specific and correct you want to be. A regex that perfectly matches the SemVer rules can be fairly long.
What I use for my projects is: /^v\d+\.\d+\.\d+$/
. It matches versions that start with a ‘v’ and is three integers separated by a decimal. It doesn’t go into betas and RCs or anything, but works for the simple stuff.
Hi @FelicianoTech thank you for the response!
Yes we currently use a regex very similar. Ours also does not support betas or rc’s, and that’s exactly why I want to use the regex from the semver website here.
If you look at the original post, I was posting about how there is a config error when using the semver regex for named capturing groups. Are you able to provide any input on that?
I was hoping to not have to worry about inventing a new regex, and just instead basically copying + tweaking the semver one.
Hello again!
I discovered two things:
- The regex may need to begin and end with
/
(unsure if this is actually true)
- The config is valid if you remove the named capturing groups, making it:
tags:
# SEE: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
# SEE: https://regex101.com/r/Ly7O1x/636
# This is that ^^ regex, except it begins with a literal v,
# starts/ends with a `/`and removes all of the named capturing
# groups
only: /^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
So, I am left wondering if CircleCI’s regex implementation supports named capturing groups.
Starting from the named capturing groups may not be your best option. Actually semver•org already shares a unnamed version:
https(:)//regex101•com/r/vkijKf/1/
You can push further and make these groups non-capturing as capturing is irrelevant in CircleCI filters context and comes with a slight overhead.
Here a version of it also allowing leading v
followed or not by a dot.
^(?:v\.?)?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?$
https(:)//regex101•com/r/csiE8k/1
Leading and trailing /
are only a pattern delimiter, as in ecmascript.