CodexBloom - Programming Q&A Platform

Regex optimization guide for Extracting Custom Version Strings in Rust - implementing Pre-release Identifiers

πŸ‘€ Views: 1267 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-11
regex rust string-manipulation Rust

I've spent hours debugging this and I'm trying to use regex in Rust to extract version strings from a list of custom version identifiers that follow the semantic versioning structure, such as `1.0.0-alpha`, `2.3.4-beta.2`, and `3.1.1+build.123`. However, my current regex fails to capture pre-release identifiers properly. Here’s the regex I'm using: ```rust let re = Regex::new(r"(\d+)\.(\d+)\.(\d+)(?:-([\w.-]+))?(?:\+([\w.-]+))?").unwrap(); ``` When I test it against the string `"2.3.4-beta.2"`, I’m only capturing the `2`, `3`, and `4`, but the pre-release identifier `beta.2` is not matched. The captures for `major`, `minor`, and `patch` work fine, but it seems like the optional part for pre-release (`-([\w.-]+)`) is not functioning as expected. I have also tried adding more specific character sets, but the regex still fails to match the pre-release identifier correctly. I get the following output when I run my code: ```plaintext "captures": ["2.3.4-beta.2", "2", "3", "4", None, None] ``` This indicates that the capturing groups for the pre-release part are returning `None`. Can someone guide to understand why the pre-release identifier is not being captured? Am I missing something in the regex syntax, or is there a better way to handle this in Rust? I'm using the `regex` crate version `1.5.4`. This is part of a larger service I'm building. Any help would be greatly appreciated! The project is a CLI tool built with Rust. I'd really appreciate any guidance on this. I'm on Debian using the latest version of Rust.