正则表达式string str="dkhj4dfj98";
Regex re="[0-9]+";
Match m=re.Matcher(str);
if(m.Success)
MessageBox.Show("包含数字");
Regex re="[0-9]+";
Match m=re.Matcher(str);
if(m.Success)
MessageBox.Show("包含数字");
例如string tempStr="5431ewr";
那怎么判断前四位就是数字?
/// 检查是否为正确整型格式
/// </summary>
/// <param name="strInt">传人参数</param>
/// <returns></returns>
public static bool CheckInt(string strInt)
{
try
{
Convert.ToInt32(strInt);
return true;
}
catch
{
return false;
}
}
string str1=null;
for(int i =0 ;i<str.Length;i++)
{
if (Char.IsDigit(str[i]))
str1+=str[i];
else
break;
}
MessageBox.Show(str1);
Regex r = new Regex (@"(?<proto>\d+)\w+" , RegexOptions.Compiled );
int numLen = r.Match (tempStr).Result("${proto}").Length ;
string str = "123er456789sdfsdf";
Match myMatch = reg.Matches(str)[0];
MessageBox.Show(myMatch.ToString());