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:
String.StartsWith("mailto:")
String.StartsWith("ftp")
It looks like a regex would be better for this. Or is there a C# way I am missing here?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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.
Method 3
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;
}
}
Method 4
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");
Method 5
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0