C#正则expression式,单引号之间的string
string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
我想要使用正则expression式之间'
引号之间的文本。
任何人都可以
像这样的东西应该这样做:
string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'"; Match match = Regex.Match(val, @"'([^']*)"); if (match.Success) { string yourValue = match.Groups[1].Value; Console.WriteLine(yourValue); }
expression式'([^']*)
:
' -> find a single quotation mark ( -> start a matching group [^'] -> match any character that is not a single quotation mark * -> ...zero or more times ) -> end the matching group
您正在使用正则expression式来匹配string中的GUID。
这是你想要的,我怀疑!
public static Regex regex = new Regex( "(\\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-"+ "([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\\}{0,1})",RegexOptions.CultureInvariant|RegexOptions.Compiled); Match m = regex.Match(lineData); if (m.Succes) { ... }
这将提取一行中第一个和最后一个单引号之间的文本:
string input = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'"; Regex regName = new Regex("'(.*)'"); Match match = regName.Match(input); if (match.Success) { string result = match.Groups[1].Value; //do something with the result }
你也可以使用积极的向前看和向后看,
string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'"; Match match = Regex.Match(val, @"(?<=')[^']*(?=')"); if (match.Success) { string yourValue = match.Groups[0].Value; Console.WriteLine(yourValue); }