-
C#正则表达式提取字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;
namespace 字符串提取
{
class Program
{
static void Main(string[] args)
{
#region 01_利用正则表达式来判断输入的是否是6位数字
//Regex regex = new Regex(@"^\d{6}$", RegexOptions.ECMAScript);
//while (true)
//{
// Console.WriteLine("请输入一个字符串");
// string postcode = Console.ReadLine();
// bool b = regex.IsMatch(postcode);
// Console.WriteLine(b);
//}
#endregion
#region 02_正则表达式提取字符串中的单个数字
//string msg = "大家好,hi,2016年8月12日。今天是个好日子,9494,咱们老百姓今个真高兴3166.886";
Match match = Regex.Match(msg, @"\d", RegexOptions.ECMAScript); //提取结果为2
//Match match = Regex.Match(msg, @"\d+", RegexOptions.ECMAScript); //提取结果为2010
//Console.WriteLine(match.Value);
//Console.ReadLine();
#endregion
#region 03_正则表达式逐个提取字符串中的所有单个数字,适合较大文件中提取字符
//string msg = "大家好,hi,2016年8月12日。今天是个好日子,9494,潇洒哥今个真高兴3166.886";
//Regex regex = new Regex(@"\d+", RegexOptions.ECMAScript);
//Match match = regex.Match(msg);
//Console.WriteLine(match.Value);
//match = regex.Match(msg,match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//Console.ReadKey();
#endregion
#region 04_正则表达式逐个提取字符串中的所有单个数字,适合较大文件中提取字符.方法2循环
string msg = "大家好,hi,2016年8月12日。今天是个好日子,9494,咱们老百姓今个真高兴3166.886";
Regex regex = new Regex(@"\d+", RegexOptions.ECMAScript);
Match match = regex.Match(msg);
while (match.Value.Length !=0)
{
Console.WriteLine(match.Value);
match = regex.Match(msg, match.Index + match.Value.Length);
}
Console.ReadKey();
#endregion
#region 05_正则表达式提取字符串中的所有数字
//string msg = "大家好,hi,2016年8月12日。今天是个好日子,9494,咱们老百姓今个真高兴3166.886";
//MatchCollection matches = Regex.Matches(msg, @"\d+", RegexOptions.ECMAScript);
//for (int i = 0; i < matches.Count; i++)
//{
// Console.WriteLine(matches[i].Value);
//}
//Console.ReadKey();
#endregion
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;
namespace 字符串提取
{
class Program
{
static void Main(string[] args)
{
#region 01_利用正则表达式来判断输入的是否是6位数字
//Regex regex = new Regex(@"^\d{6}$", RegexOptions.ECMAScript);
//while (true)
//{
// Console.WriteLine("请输入一个字符串");
// string postcode = Console.ReadLine();
// bool b = regex.IsMatch(postcode);
// Console.WriteLine(b);
//}
#endregion
#region 02_正则表达式提取字符串中的单个数字
//string msg = "大家好,hi,2016年8月12日。今天是个好日子,9494,咱们老百姓今个真高兴3166.886";
Match match = Regex.Match(msg, @"\d", RegexOptions.ECMAScript); //提取结果为2
//Match match = Regex.Match(msg, @"\d+", RegexOptions.ECMAScript); //提取结果为2010
//Console.WriteLine(match.Value);
//Console.ReadLine();
#endregion
#region 03_正则表达式逐个提取字符串中的所有单个数字,适合较大文件中提取字符
//string msg = "大家好,hi,2016年8月12日。今天是个好日子,9494,潇洒哥今个真高兴3166.886";
//Regex regex = new Regex(@"\d+", RegexOptions.ECMAScript);
//Match match = regex.Match(msg);
//Console.WriteLine(match.Value);
//match = regex.Match(msg,match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//match = regex.Match(msg, match.Index + match.Value.Length);
//Console.WriteLine(match.Value);
//Console.ReadKey();
#endregion
#region 04_正则表达式逐个提取字符串中的所有单个数字,适合较大文件中提取字符.方法2循环
string msg = "大家好,hi,2016年8月12日。今天是个好日子,9494,咱们老百姓今个真高兴3166.886";
Regex regex = new Regex(@"\d+", RegexOptions.ECMAScript);
Match match = regex.Match(msg);
while (match.Value.Length !=0)
{
Console.WriteLine(match.Value);
match = regex.Match(msg, match.Index + match.Value.Length);
}
Console.ReadKey();
#endregion
#region 05_正则表达式提取字符串中的所有数字
//string msg = "大家好,hi,2016年8月12日。今天是个好日子,9494,咱们老百姓今个真高兴3166.886";
//MatchCollection matches = Regex.Matches(msg, @"\d+", RegexOptions.ECMAScript);
//for (int i = 0; i < matches.Count; i++)
//{
// Console.WriteLine(matches[i].Value);
//}
//Console.ReadKey();
#endregion
}
}
}