Reputation: 2145
I need to extract version information from the following string
993-756-V01 ver 02a
What regex I need to use in order to extract 01 and 02 from 993-756-V01 ver 02a
I tried to extract the version substring i.e. V01 ver 02a with the below regex.
[A-Z][0-9]{2} ver [0-9]{2}
But is there a more direct way to extract the information
Upvotes: 1
Views: 147
Reputation: 11233
Another short possibilility i see is to use the below pattern:
V(\d+)\D+(\d+)
Here:
V = Literal `V` capital alphabet.
(\d+) = Captures any digit from 0-9 following `V` character.
\D+ = Anything except digit next to digits. (A way to find the next numeric part of your version number)
(\d+) = Captures any digit from 0-9 following non-digit characters.
Upvotes: 1
Reputation: 626748
You may use a regex with named capturing groups to capture the information you need. See the following code:
var s = "993-756-V01 ver 02a";
var result = Regex.Match(s, @"(?<major>[0-9]+)\s+ver\s+(?<minor>[0-9]+)");
if (result.Success)
{
Console.WriteLine("Major: {0}\nMinor: {1}",
result.Groups["major"].Value, result.Groups["minor"].Value);
}
See the C# demo
Pattern details:
(?<major>[0-9]+)
- Group "major" capturing 1+ digits\s+
- 1+ whitespacesver\s+
- ver
substring and then 1+ whitespaces(?<minor>[0-9]+)
- Group "minor" capturing 1+ digitsUpvotes: 1