我有这样的输入格式:
{random string} + " " + {integer}
例如
a) stringInput 43
b) abcdefghijkl 89
如何使用正则表达式验证这种格式?
我遇到了一个问题:字符串的正则表达式是什么?
public bool IsValidString(string s)
{
string[] strs = s.Split(' ');
int i = 0;
if (strs.Length != 2)
return false;
return (int.TryParse(strs[1], out i);
}
You don't really need to use regex for this if you don't understand it, just an alternative method if you wanted to have a look. It may be easier to read, I personally find Regex
very hard to read.
试试这个,
bool _result = Regex.IsMatch(yourStr, @"^[A-Za-z]+\s\d+$");
正则表达式模式的解释,
自卫队
^
[A-Za-z]+
+
A-Z
a-z
\s
\d+
+
$
应该管用
bool _result = Regex.IsMatch(yourStr,@"^\w+?.\d+?$");