1 Functional Requirements
Match string "M1.large (vcpu 2,ram 4G) |c95f5529-47e8-46d4-85da-319eb9905a9b". Target string vcpu: "2", Ram: "4", ID: "c95f5529-47e8-46d4-85da-319eb9905a9b".
2 Code implementation
Using System;
Using System.Text.RegularExpressions;
Namespace Regexdemo
{
class program
{
static void Main (string[] args)
{
var flavorref = " M1.large (vcpu 2,ram 4G) |c95f5529-47e8-46d4-85da-319eb9905a9b ";
Regex regex = new Regex (@ <=) [^ \,g]+ (? =\,| G) | (? <=\|) [^\|] +");
MatchCollection mc = Regex. Matches (flavorref);
var CPU = mc[0]. Value; Cpu:2
var ram = mc[1]. Value; Ram:4
var id = mc[2]. Value; flavorid:c95f5529-47e8-46d4-85da-319eb9905a9b
Console.WriteLine ("CPU: + CPU +" "+" RAM: "+ Ram +" "+" ID: "+ I d);
Console.readkey ();}}
3 Regular Expression Specification reference
+: Matches the preceding subexpression one or more times. For example, "zo+" can Match "Zo" and "Zoo", but cannot match "Z". + is equivalent to {1,}.
X|y: Matches x or Y. For example, "Z|food" can match "Z" or "food". "(z|f) Ood" matches "Zood" or "food". [^XYZ]: Negative character set combination. Matches any characters that are not included. For example, "[^ABC]" can match "P" in "plain".
(? =pattern): Positive and positive check. For example, the Windows (? =95|98| nt|2000) "Can match" Windows "in" Windows2000 ".
(? <=pattern): Reverse positive check. For example, "(? <=95|98| nt|2000) Windows "can match Windows" in "2000Windows".
4 attached: Regular expression reference manual
Http://tool.oschina.net/uploads/apidocs/jquery/regexp.html