string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
我想获得使用正则表达式的'
引号之间的文本。C#正则表达式,单引号之间的字符串
任何人都可以吗?
string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";
我想获得使用正则表达式的'
引号之间的文本。C#正则表达式,单引号之间的字符串
任何人都可以吗?
像这样的东西应该这样做:
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);
}
表达'([^']*)
的说明:
' -> 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
您正在寻找匹配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
}
如果你有'a','b'这个人会得到一串“a”,“b”而不是可能的“a”。 @弗雷德里克将会这样做。 – 2012-07-18 17:18:06
你可以用积极的前瞻和回顾后也
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);
}
它非常有用的解释。但为什么群[1]? – liang 2013-09-16 05:23:09
@liang第一组(match.Groups [0]')将包含整个正则表达式匹配的完整字符串。这意味着它还包含主要引用字符。 'match.Groups [1]'包含正则表达式中的第一个匹配组,因此这是我们想要使用的值。 – 2013-09-16 06:17:54
不知道团体,但这正是帮助很多! +1 – feldeOne 2017-09-05 10:25:08