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-V 01 ver 02 a
I tried to extract the version substring ie 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
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+ whitespaces ver\\s+
- ver
substring and then 1+ whitespaces (?<minor>[0-9]+)
- Group "minor" capturing 1+ digits 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.