c#简单正则表达式~

apxar 2012-12-30 12:03:25


①元旦:</strong>1月1日
②元旦:</strong>2007年12月30日



问题:能不能帮忙写一个正则表达式,可以匹配上面的2种情况 ,


结果:最后要获取的信息是(就是要获取几月几日):
1 1
12 30
...全文
109 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2012-12-30
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = @"①元旦:</strong>1月1日
②元旦:</strong>2007年12月30日";
            var result = Regex.Matches(s, @"元旦:\</strong\>(\d{4}年){0,1}(?<m>\d{1,2})月(?<d>\d{1,2})日");
            foreach (Match item in result)
                Console.WriteLine("{0} {1}", item.Groups["m"], item.Groups["d"]);
        }
    }
}
1 1 12 30 Press any key to continue . . .
  • 打赏
  • 举报
回复
(?<=</strong>(\d+年)?)(?<month>\d+)月(?<day>\d+)日
你的选择B 2012-12-30
  • 打赏
  • 举报
回复

            string[] strs = new string[] { "元旦:</strong>1月1日", "元旦:</strong>2007年12月30日" };
            Regex regex = new Regex(@"(?<Month>\d+)月(?<Day>\d+)日");

            foreach (string str in strs)
            {
                Match match = regex.Match(str);
                Console.WriteLine(match.Groups["Month"].Value + " " + match.Groups["Day"].Value);
            }

dalmeeme 2012-12-30
  • 打赏
  • 举报
回复
 string s = @"①元旦:</strong>1月1日
②元旦:</strong>2007年12月30日";
MatchCollection matches=Regex.Matches(s,@"(?is)\w+:</strong>(?:\d{4}年)?(?<month>\d{1,2})月(?<date>\d{1,2})日");
foreach(Match match in matches)
    Console.WriteLine("{0}  {1}",match.Groups["month"].Value,match.Groups["date"].Value);
手写的,没调试过。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧