在我的源文件中有很多类似以下这样的shortcut:
Ctrl+B E
Ctrl+M
Ctrl+F1
Ctrl+Shift+E
Alt+M
Alt+D
Ctrl+Alt+M E
...我需要取出这样的shortcut, 我自己写了一个,但是没有达到要求。
(Ctrl\+)?(Shift\+)?(Alt\+)?[a-zA-Z0-9]+不知道各位有什么更好的正则表达式呢?希望各位不吝赐教。谢谢

解决方案 »

  1.   

    Ctrl+B E是匹配Ctrl+B E还是匹配Ctrl+B?
      

  2.   

    @"((Ctrl|Shift|Alt)\+)+[a-zA-Z0-9](\s[a-zA-Z0-9])+"
      

  3.   

    最后一个 + 改为 * :
    @"((Ctrl|Shift|Alt)\+)+[a-zA-Z0-9](\s[a-zA-Z0-9])*"
      

  4.   

    @"((Ctrl|Shift|Alt)\+)(+[a-zA-Z0-9])?"
      

  5.   

    @"((Ctrl|Shift|Alt)\+)(+[a-zA-Z0-9])+(\s[a-zA-Z0-9])+"
      

  6.   

    改了下空军的
    可以匹配LZ给的例子@“((Ctrl|Shift|Alt)\+)+[a-zA-Z0-9]{1,3}(\s[a-zA-Z0-9]{1,3})*”
      

  7.   

    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[] str = new string[] { "Ctrl+B E", "Ctrl+M", "Ctrl+F1", "Ctrl+Shift+E", "Alt+M", "Alt+D", "Ctrl+Alt+M E" };
                Regex re = new Regex(@"(Ctrl|Alt)\+(Shift\+|Alt\+)?[A-Z0-9\s]*");
                foreach (string s in str)
                {
                    if (re.Match(s).Success)
                        Console.WriteLine(re.Match(s).Value);
                }        }
        }
    }
      

  8.   


    谢谢你的帮助,不过在你的基础上改动了一下
    @"((Ctrl|Shift|Alt)\+)+[a-zA-Z0-9]+(\s[a-zA-Z0-9])*"因为我要匹配F1揭帖 给分  大家都有
      

  9.   

    后面“+”改成“*”
    (Ctrl\+)?(Shift\+)?(Alt\+)?[a-zA-Z0-9]*