110,539
社区成员
发帖
与我相关
我的任务
分享
string str = "var stock_str='万 科A,8.83,8.80,2009-05-05,15:05:28';";
//问题一:
"(?<=')([^,]+),([^,]+),([^,]+),([^']+)" //Group[1-4] 即为所求
/*
Group[1].Value: 万 科A
Group[2].Value: 8.83
Group[3].Value: 8.80
Group[4].Value: 2009-05-05,15:05:28
*/
//问题二:
//正则表达式是匹配连续的字符串 要想将不连续的字符串拼接 只能在匹配后再操作
"(?<=')([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)"
/*
Group[1].Value: 万 科A
Group[2].Value: 8.83
Group[3].Value: 8.80
Group[4].Value + " " + Group[5].Value: 2009-05-05 15:05:28 */
//问题一:
"(?<=')([^,]+),([^,]+),([^,]+),([^']+)" //Group[1-4] 即为所求
/*
Group[1].Value: 万 科A
Group[2].Value: 8.83
Group[3].Value: 8.80
Group[4].Value: 2009-05-05,15:05:28
*/
//问题二:
//正则表达式是匹配连续的字符串 要想将不连续的字符串拼接 只能在匹配后再操作
"(?<=')([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)"
/*
Group[1].Value: 万 科A
Group[2].Value: 8.83
Group[3].Value: 8.80
Group[4].Value + " " + Group[5].Value: 2009-05-05,15:05:28 */
using System;
using System.Text.RegularExpressions;
namespace regextest
{
class Program
{
static void Main(string[] args)
{
string temp = "var stock_str='万 科A,1.83,0.80,2800-12-10,15:05:28;'";
Regex rx = new Regex("^.+'(?<name>.+),(?<price1>(?(?=[1-9])[1-9][\\d]*|0)\\.\\d{2}),(?<price2>(?(?=[1-9])[1-9][\\d]*|0)\\.\\d{2}),(?<date>((?(?=1)19\\d{2}|[2-9]\\d{3}))-(?(?=0)0[1-9]|1[0-2])-[0-3]\\d),(?<time>\\d{2}:\\d{2}:\\d{2});'$");
MatchCollection mcfinal = rx.Matches(temp);
GroupCollection gcfinal;
foreach (Match mfinal in mcfinal)
{
gcfinal = mfinal.Groups;
Console.WriteLine(gcfinal["name"].Value);
Console.WriteLine(gcfinal["price1"].Value);
Console.WriteLine(gcfinal["price2"].Value);
Console.WriteLine(gcfinal["date"].Value);
Console.WriteLine(gcfinal["time"].Value);
}
Console.ReadKey();
}
}
}
(?<=')[\u4e00-\u9fa5\sA]*|(?<=\d+\.\d\d\,\d+\.\d\d\,)([0-9\.\-\:\,]*)|([0-9\.]*)
(?<=')[\u4e00-\u9fa5\sA]*|([0-9\.\-\:]*)
(?<=')[\u4e00-\u9fa5\sA]*|(?<=\d\.\d\d\,\d\.\d\d\,)([0-9\.\-\:\,]*)|([0-9\.]*)
(?<=')[\u4e00-\u9fa5\sA]*|([0-9\.\-\:]*)
1 ^([\u4e00-\u9fa5\sAB]*)|(?<=\d\.\d\d\,\d\.\d\d\,)([0-9\.\-\:\,]*)$|([0-9\.]*)
2 ^([\u4e00-\u9fa5\sAB]*)|([0-9\.\-\:]*)
string str1 = "var stock_str='万 科A,8.83,8.80,2009-05-05 15:05:28';";
Match m1 = Regex.Match(str1, @".+?'([^,]*?),([^,]*?),([^,]*?),([^,]*)'");
if (m1.Success)
{
MessageBox.Show(m1.Groups[1].Value+System.Environment.NewLine+
m1.Groups[2].Value+System.Environment.NewLine+
m1.Groups[3].Value+System.Environment.NewLine+
m1.Groups[4].Value);
}
string str2 = "var stock_str='万 科A,8.83,8.80,2009-05-05,15:05:28';";
Match m2 = Regex.Match(str2, @".+?'([^,]*?),([^,]*?),([^,]*?),([^,]*),([^,]*)'");
if (m2.Success)
{
MessageBox.Show(m2.Groups[1].Value + System.Environment.NewLine +
m2.Groups[2].Value + System.Environment.NewLine +
m2.Groups[3].Value + System.Environment.NewLine +
m2.Groups[4].Value + " " + m2.Groups[5].Value);
}