Regex Failing to Validate Version Strings in Java - Handling Pre-release Tags Incorrectly
I've looked through the documentation and I'm still confused about I'm currently working on a Java application that requires validating version strings according to semantic versioning (SemVer). The format I'm trying to enforce looks like this: `MAJOR.MINOR.PATCH[-PRERELEASE][+BUILD]`, for example, `1.0.0-alpha+001`. I implemented the following regex pattern: ```java String regex = "^(\\d+)\\.(\\d+)\\.(\\d+)(-(\\w+))?(\\+(\\w+))?$"; ``` While this pattern correctly matches basic versions like `1.0.0`, it seems to fail when I include the pre-release tags. For instance, it matches `1.0.0-alpha` but incorrectly fails for `1.0.0-alpha+001`, returning `false` when I expect it to match. I've tested it using Java's `Pattern` and `Matcher` classes: ```java import java.util.regex.Pattern; import java.util.regex.Matcher; public class VersionValidator { public static void main(String[] args) { String version = "1.0.0-alpha+001"; String regex = "^(\\d+)\\.(\\d+)\\.(\\d+)(-(\\w+))?(\\+(\\w+))?$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(version); System.out.println(matcher.matches()); // This prints false } } ``` I've tried adjusting the regex to account for multiple pre-release tags or different characters, but it just results in more unexpected matches or failures. Am I missing something in my regex logic, or is there a need to rethink how I'm structuring it to account for the build metadata? Also, are there any performance considerations I should keep in mind when using regex for such validations in a production environment? Any insights would be greatly appreciated! This is part of a larger CLI tool I'm building.