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

后端 未结 5 1080
小鲜肉
小鲜肉 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: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.

提交回复
热议问题