我有以下语句,我想提取 Video[0] 和 title[0] 的值。
String text = "< php $video[0]='aEOqMqVWB5s';$title[0]='www.yahoo.com'; ?>";
如何使用 Regex.Matches 和 Groups[0].Value?所以在这个例子中,第一组是aEOqMqVWB5s
,第二组是www.yahoo.com
。
提前致谢。
括号将创建您需要的 2 个组。此外,您必须转义以下字符:$,[,],? 你可以用“\”来做到这一点。所以,你的正则表达式看起来像: "< php \$video\[0\]='(.*)';\$title\[0\]='(.*)'; \?>"
您可以使用此模式:
\$(?:video|title)\[0\]='(.*?)'
C# 示例:(未经测试,让您了解如何开始)
MatchCollection mcol = Regex.Matches(inputStr,"\$(?:video|title)\[0\]='(.*?)'");
foreach(Match m in mcol)
{
Debug.Print(m.ToString()); // See Output Window
// Here you can use m.Groups[0].Value or m.Groups[1].Value
// adjust your loop accordingly
}
\$ = looks for `$` character, need to escape since it has a specific meaning for regex enine.
(?:video|title) = match for either word `video` or `title`, Don't capture group.
\[0\]= = looks for literal `[` followed by `0` ollowed by `]` and `=`.
'(.*?)' = lazy match for anything enclosed by single quote `()` makes a group here.