C#⽤正则表达式判断字符串是否为纯数字
Regex regex = new System.Text.RegularExpressions.Regex("^(-?[0-9]*[.]*[0-9]{0,3})$");
string itemValue="abc123"
bool b=regex.IsMatch(itemValue);
if(b==true)
{
  //是纯数字
}
else
{
  //不是纯数字
}
下⾯这段代码是判断是否为纯数字,如果是就加1,如果是以字母开头的数字字符串,字母不变,后⾯数字加1
1class Program
2    {
3static void Main(string[] args)
4        {
5string str = Console.ReadLine();
6string strTmp = "";
7if (IsNumeric(str))
8            {
9                strTmp = (ConvertToLong(Convert.ToInt32(str) + 1).ToString().PadLeft(str.Length, '0'));
10            }
11else
12            {
13int iNum = str.Length;
14int j = 0;
15for (int i = 0; i < iNum; i++)
16                {
17if (!IsNumeric(str[i].ToString()))
18                    {
19                        j++;
20                    }
21                }
22                strTmp = str.Substring(0, j) + (ConvertToLong(Convert.ToInt32(str.Substring(j, str.Length - j)) + 1).ToString().PadLeft(str.Substring(j, str.Length - j).Length, '0'));
23            }
24            Console.WriteLine(strTmp);
25            Console.ReadKey();
26        }
27public static bool IsNumeric(string itemValue)
28        {
29return (IsRegEx("^(-?[0-9]*[.]*[0-9]{0,3})$", itemValue));
30        }
31public static long ConvertToLong(object value)
32        {
33if (value == null || value.ToString().Trim() == "")
34            {
35return0;
36            }
37
38long nValue;
39long.TryParse(value.ToString(), out nValue);
40
41return nValue;
42        }
43public static bool IsRegEx(string regExValue, string itemValue)
44        {
45try
46            {
47                Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
48if (regex.IsMatch(itemValue)) return true;
python正则表达式判断
49else return false;
50            }
51catch (Exception)
52            {
53return false;
54            }
55finally
56            {
57            }
58        }
59    }
View Code