using System; using System.Text.RegularExpressions;namespace WondowsConsole { class Program { static void Main(string[] args) { string text = "abc##$%25"; string pattern = "[^0-9a-zA-Z]+"; Console.WriteLine(Regex.Replace(text,pattern,"")); } } }
static void Main(string[] args) { string text = "a&*bc##$%25"; string pattern = "[0-9a-zA-Z]+"; Regex reg = new Regex(pattern); foreach (Match m in reg.Matches(text)) { Console.WriteLine(m.Value); } }输出: a bc 25
static void Main(string[] args) { string text = "a&*bc##$%25"; Regex reg = new Regex(@"\d{1,}|[a-zA-Z]{1,}"); foreach (Match m in reg.Matches(text)) { Response.Write(m.Value+"<br/>"); } }
string a="92fm92%3un#fm983&ufm";
string output = Regex.Replace(a,"[^0-9a-zA-Z]+",""); // ="92fm923unfm983ufm";
或者
MatchCollection mc = Regex.Matches(a,"[0-9a-zA-Z]+");
foreach(Match m in mc)
{
Console.WriteLine(m.Value); // 92fm92,3un,fm983,ufm
}
using System;
using System.Text.RegularExpressions;namespace WondowsConsole
{
class Program
{
static void Main(string[] args)
{
string text = "abc##$%25";
string pattern = "[^0-9a-zA-Z]+";
Console.WriteLine(Regex.Replace(text,pattern,""));
}
}
}
static void Main(string[] args)
{
string text = "a&*bc##$%25";
string pattern = "[0-9a-zA-Z]+"; Regex reg = new Regex(pattern);
foreach (Match m in reg.Matches(text))
{
Console.WriteLine(m.Value);
}
}输出:
a
bc
25
{
string text = "a&*bc##$%25";
Regex reg = new Regex(@"\d{1,}|[a-zA-Z]{1,}");
foreach (Match m in reg.Matches(text))
{
Response.Write(m.Value+"<br/>");
}
}
(@"\d{1,}|[a-zA-Z]{1,}|\w{1,}";
2. 正则表达式,楼主的问题比较简单,有点杀鸡用牛刀
3. 正解: Convert.ToInt32,
Convert.ToInt32 方法 (String, IFormatProvider),可以提供格式