mirror of
https://github.com/semver/semver.git
synced 2026-07-11 03:53:53 -05:00
[GH-ISSUE #981] modern/extended POSIX-compliant SemVer RegEx (for bash) #5723
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @har7an on GitHub (Oct 14, 2023).
Original GitHub issue: https://github.com/semver/semver/issues/981
Hello,
today I was writing an application for a bit of CI-infrastructure of mine that needs to handle semver numbers from applications. Since I prefer to write my CI code in plain bash, I came up with a regex that one can perform in bash to match semver. This differs slightly from the example for numbered capture groups since POSIX regex (which is what bash uses), as far as I know, has no concept of non-matching capture groups. Here's what I came up with:
The individual parts are captured as follows:
Here is the fully-expanded regex pattern:
Maybe this will save someone else an hour of playing with regular expressions. :)
@har7an commented on GitHub (Oct 14, 2023):
I see the website code is hosted in this repo as well. If there's interest, I'll happily turn this into a PR to add a third regular expression at the bottom of the page.
@jwdonahue commented on GitHub (Oct 17, 2023):
You will find a test string here: https://regex101.com/r/vkijKf/1/
How does your regex perform against the valid/invalid data?
@har7an commented on GitHub (Oct 18, 2023):
Oh right, sorry I forgot to mention this. It passes the tests, here's a sample code to run for anyone interested:
and here's the output:
@stas-at-ibm commented on GitHub (Nov 19, 2023):
You made my day! I was going nuts yesterday trying to make it work in bash 😆
@PepekT commented on GitHub (Jan 8, 2024):
Great job, thanks a lot!
I have one question, could you please explain why:
PRE_RELEASEis accessed with index 5BUILD_METADATAis accessed with index 10Thank you
@har7an commented on GitHub (Jan 9, 2024):
@PepekT
PRE_RELEASEis index 5 because index 4 matches thePRE_RELEASEincluding the-preceding that groupBUILD_METADATAis index 10 because the group matching it is the 10th (counting all opening braces(from the beginning of the pattern). Here, again, the 9th group also matches the preceding+, which we don't want.@jwdonahue Is this good to go?
@jwdonahue commented on GitHub (Jan 9, 2024):
I think it all looks great, and on behalf of bash coders everywhere, thank you for the effort!
My bash foo is weak and it's really not up to me (not a maintainer).
Does bash process only ASCII or at least just the lower 128 code points of UTF-8?
If
$Donly matches [0..9] (ASCII code points 48..57), then it looks pretty good to me. Unfortunately, our current regex's can match outside that range for\dwhen Unicode is enabled in some environments (there's a bug for that around here somewhere).I have made a close inspection and I don't see anything wrong with it. My main concern, as with all regex's, is whether there are any potential perf or run-away concerns wrt the bash regex implementation and this particular regex. The test data we have catches the potential issues, such as excessive back tracking, non-termination or failure to match due to timeouts, that we know about with the other two implementations and I suspect they cover that aspect for regex's in general, but like I said, my bash foo is weak.
Since there do not seem to be any POSIX compatible regex test sites to share this on, I think the next step would be to put that in a dedicated github repo; with at least a short readme file, and then issue a PR here, with proposed changes to the FAQ that includes a link back to the repo. After a round or two of review of those changes, you should get the attention of the maintainers.
@har7an commented on GitHub (Jan 20, 2024):
Alright, the repo is here: https://github.com/har7an/bash-semver-regex
Thanks for the feedback @jwdonahue !
@markopesevski commented on GitHub (Dec 12, 2024):
@har7an ❤️ thank you very much for this. Saved me at least a couple hours of fiddling with it myself.
@ljharb commented on GitHub (Dec 12, 2024):
@har7an can you adapt your bash code to work in posix shells?
@har7an commented on GitHub (Jan 8, 2025):
@ljharb I guess so, but I haven't dealt with POSIX-compliance before. Can you help me out a bit?
@ljharb commented on GitHub (Jan 8, 2025):
Some potential things I can see off the top of my head:
declare(POSIX doesn't have arrays), thefunctionkeyword,[[ ]]syntax (POSIX only has[ ]).Re tools, shellcheck is the defacto tool for this.
@har7an commented on GitHub (Jan 8, 2025):
I see. I'll poke around in the next few days, but I can't promise anything. Assuming POSIX doesn't allow arrays at all, I don't see a way how this would work (given that
BASH_REMATCHis an array).Thanks for the challenge, though. :)
@ljharb commented on GitHub (Jan 8, 2025):
It's definitely a challenge :-) if it helps, it'd end up being used in nvm if it was reliable and compact enough.
@peppergrayxyz commented on GitHub (Jan 13, 2025):
I forked the bash version from @har7an and create a POSIX shell version. It uses only built-in commands (no regex) and should run on anything that has a shell: https://github.com/peppergrayxyz/posix-shell-semver
@jwdonahue commented on GitHub (Jan 13, 2025):
Slightly off topic: this discussion on POSIX reminded me of this cartoon:
https://xkcd.com/927/
Back on topic: Any well written and thoroughly tested solution, that does not use a regex, is likely to be far superior to one that does. Perhaps we need to add some well vetted script examples to the FAQ?
Correction, a list of URL's pointing to well maintained script sites/libraries.