嵌套的正则表达式

时间:2013-09-10 07:48:28

标签: regex

我正在尝试编写一个匹配并捕获完整日期以及日期年份的正则表达式。

示例

TEXT: JAN 2013
CAPTURE: JAN 2013, 2013

我试图使用:

[a-z]{3}\s(\d{4},*){2}

但是这个不起作用。如果有人可以提供帮助。

2 个答案:

答案 0 :(得分:1)

以下正则表达式为您提供了两个捕获组:

([A-Z]{3}\s(\d{4}))

第一个将包含" JAN 2013"和第二个" 2013"。

Perl中的示例:

$text = "JAN 2013";
@m = $text =~ /([A-Z]{3}\s(\d{4}))/;
print "First capture: $m[0]\n";
print "Second capture: $m[1]\n";

输出:

First capture: JAN 2013
Second capture: 2013

答案 1 :(得分:0)

C#中的代码用于您想要的内容(根据评论)。

对于参考文献,您可以查看 MSDN - 正则表达式语言 - 快速参考,此处http://msdn.microsoft.com/en-us/library/az24scfc.aspx

Regex r = new Regex(@"\A([a-z]{3}\s+(\d{4}))\Z",
                    RegexOptions.IgnoreCase);

MatchCollection match = r.Matches(text);
if (match.Count == 1)
{
     GroupCollection captures = match[0].Groups;
     String theCaptureYouWanted = captures[1] + ", " + captures[2];
     ...
相关问题