Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide example RegEx #16

Closed
serverhorror opened this issue Feb 25, 2010 · 6 comments
Closed

Provide example RegEx #16

serverhorror opened this issue Feb 25, 2010 · 6 comments

Comments

@serverhorror
Copy link

Given that regex is available in all? languages mentioning the best known dialects and corresponding regular expressions to match would be nice:

Python 2.x:

^v?(?P\d+)\.(?P\d+)\.?P\d+)~?(?P[a-z]\w+[\d+])?$
@serverhorror
Copy link
Author

And now with corrected tags:

^v?(?P<major>\d+)\.(?P<minor>\d+)\.?P<patchlevel>\d+)~?(?P<special>[a-z]\w+[\d+])?$

@tbull
Copy link

tbull commented Apr 20, 2011

So I guess Python doesn't use PCRE, does it?
Anyway, here the expressions from my Java implementation (not yet published):

protected static final Pattern special_pattern = 
    Pattern.compile("[A-Za-z][0-9A-Za-z-]*");
protected static final Pattern semver_pattern =
    Pattern.compile("v?(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?([A-Za-z][0-9A-Za-z-]*)?");

This expression also accepts version numbers with the minor and/or patch number omitted, e.g. "1.2" or "1alpha".

@serverhorror
Copy link
Author

I think it does. What you get from my regex is a dictionairy (key/value pairs) where the keys are "major", "minor", "patchlevel", "special" so that you can access the different parts by name and not by index from the regex grouping.

@JakeWharton
Copy link

@serverhorror your regex doesn't adhere to rule 3 properly. Something like [a-zA-Z][-a-zA-Z0-9]* is better for the body of the 'special' clause.

The rule doesn't explicitly declare it, but it seems odd to allow a trailing dash (-). Perhaps this should be brought up as another issue. This can be included by changing the 'special' clause to ([a-zA-Z][-a-zA-Z0-9]*)(?<!-).

@mojombo
Copy link
Contributor

mojombo commented Sep 7, 2011

I think this is outside the scope of SemVer. The spec should state the requirements clearly enough to easily construct a RegEx, and if it does not then that is a failure of the spec and should be addressed separately.

@mojombo mojombo closed this as completed Sep 7, 2011
taringamberini pushed a commit to taringamberini/semver.org that referenced this issue Aug 16, 2013
@krainboltgreene
Copy link

I know this is old, but would it be beneficial to have "examples" enough to that implementation builders have test cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants