如何使用RegEx?

Ash*_*shu 4 .net c# regex

我没有使用RegEx所以请原谅...

我有一个字符串:

string str = "https://abce/MyTest";
Run Code Online (Sandbox Code Playgroud)

我想检查特定字符串是以"https://"开头还是以"/ MyTest"结尾.

我该如何实现这一目标?

R. *_*des 17

这个正则表达式:

^https://.*/MyTest$
Run Code Online (Sandbox Code Playgroud)

会做你要求的.

^ 匹配字符串的开头.

https:// 将完全匹配.

.*将匹配任何类型的任何数量的字符(*部分)(.部分).如果要确保中间至少有一个字符,请.+改用.

/MyTest 恰好匹配.

$ 匹配字符串的结尾.

要验证匹配,请使用:

Regex.IsMatch(str, @"^https://.*/MyTest$");
Run Code Online (Sandbox Code Playgroud)

有关MSDN Regex页面的更多信息.


Nol*_*rin 9

请尝试以下方法:

var str = "https://abce/MyTest";
var match = Regex.IsMatch(str, "^https://.+/MyTest$");
Run Code Online (Sandbox Code Playgroud)

^标识符的字符串的开头相匹配,而$标识符字符串的末尾匹配.该.+位仅表示任何字符序列(空序列除外).

当然,您需要为此导入System.Text.RegularExpressions名称空间.