Regex pattern for checking if a string starts with a certain substring?

后端 未结 5 1077
小鲜肉
小鲜肉 2020-12-05 22:50

What\'s the regular expression to check if a string starts with \"mailto\" or \"ftp\" or \"joe\" or...

Now I am using C# and code like this in a big if with many ors

相关标签:
5条回答
  • 2020-12-05 23:09

    For the extension method fans:

    public static bool RegexStartsWith(this string str, params string[] patterns)
    {
        return patterns.Any(pattern => 
           Regex.Match(str, "^("+pattern+")").Success);
    }
    

    Usage

    var answer = str.RegexStartsWith("mailto","ftp","joe");
    //or
    var answer2 = str.RegexStartsWith("mailto|ftp|joe");
    //or
    bool startsWithWhiteSpace = "  does this start with space or tab?".RegexStartsWith(@"\s");
    
    0 讨论(0)
  • 2020-12-05 23:11

    I really recommend using the String.StartsWith method over the Regex.IsMatch if you only plan to check the beginning of a string.

    • Firstly, the regular expression in C# is a language in a language with does not help understanding and code maintenance. Regular expression is a kind of DSL.
    • Secondly, many developers does not understand regular expressions: it is something which is not understandable for many humans.
    • Thirdly, the StartsWith method brings you features to enable culture dependant comparison which regular expressions are not aware of.

    In your case you should use regular expressions only if you plan implementing more complex string comparison in the future.

    0 讨论(0)
  • 2020-12-05 23:14

    You could use:

    ^(mailto|ftp|joe)
    

    But to be honest, StartsWith is perfectly fine to here. You could rewrite it as follows:

    string[] prefixes = { "http", "mailto", "joe" };
    string s = "joe:bloggs";
    bool result = prefixes.Any(prefix => s.StartsWith(prefix));
    

    You could also look at the System.Uri class if you are parsing URIs.

    0 讨论(0)
  • 2020-12-05 23:14

    The StartsWith method will be faster, as there is no overhead of interpreting a regular expression, but here is how you do it:

    if (Regex.IsMatch(theString, "^(mailto|ftp|joe):")) ...
    

    The ^ mathes the start of the string. You can put any protocols between the parentheses separated by | characters.

    edit:

    Another approach that is much faster, is to get the start of the string and use in a switch. The switch sets up a hash table with the strings, so it's faster than comparing all the strings:

    int index = theString.IndexOf(':');
    if (index != -1) {
      switch (theString.Substring(0, index)) {
        case "mailto":
        case "ftp":
        case "joe":
          // do something
          break;
      }
    }
    
    0 讨论(0)
  • 2020-12-05 23:35

    The following will match on any string that starts with mailto, ftp or http:

     RegEx reg = new RegEx("^(mailto|ftp|http)");
    

    To break it down:

    • ^ matches start of line
    • (mailto|ftp|http) matches any of the items separated by a |

    I would find StartsWith to be more readable in this case.

    0 讨论(0)
提交回复
热议问题