字符串如下:子串1<pic_子串2>子串3<pic_子串4><pic_子串5>子串6...要拆分成如下有序子串:子串1
子串2
子串3
子串4
子串5
子串6即
每一对<pic_ 和>之间的内容算一个子串
每两对<pic_...>之间如果有内容也算作一个子串应该怎么做
子串2
子串3
子串4
子串5
子串6即
每一对<pic_ 和>之间的内容算一个子串
每两对<pic_...>之间如果有内容也算作一个子串应该怎么做
解决方案 »
- wcf多线程
- 知道怎么根据数据库的用户名密码来判断是否登录成功
- 继承DataGridViewColumn的自定义列,在DataGridView的columns中能显示,他用的是什么机制?
- 遇到一个构造函数的问题,求救高手???
- 一个简单的打印关题
- 刚从VB.net转到C#,一直不明白这句话是怎么回事?
- WIN2008重装后,发现屏幕截图保存的图片小多了
- 我用WebClient下载文件,怎么得到要下载文件的大小
- 关于打印的问题????
- 举手之劳!请大家写一个ADO.net连接SQL Server的数据库连接字符串
- 。。。用vs2003打包winform,怎么把.NET框架打进去?并且能方便安装的
- 关于读取xml问题!
{
string s = "子串1<pic_子串2>子串3<pic_子串4><pic_子串5>子串6";
string[] haha = Regex.Replace(s, "(<pic_|>){1,}", "|", RegexOptions.IgnoreCase).Split('|');
}
foreach(Match m in Regex.Matches(s,@"(?<=<pic_|>)[^<>]+"))
{
m.Value;//
}
{
string s="子串1<pic_子串2>子串3<pic_子串4><pic_子串5>子串6";
string[]ss=s.Split(new string[]{"<",">","pic_"},StringSplitOptions.RemoveEmptyEntries);
ss.ToList().ForEach(t=>Console.WriteLine(t));
}
/*
子串1
子串2
子串3
子串4
子串5
子串6*/
子串2 pic
子串3 normal
子串4 pic
子串5 pic
子串6 normal
{
string s = "子串1<pic_子串2>子串3<pic_子串4><pic_子串5>子串6";
foreach (Match m in Regex.Matches(s, @"(?<=<(pic_)|(>|^))[^<>]+"))
{
if (m.Groups[1].Success) Console.WriteLine(m.Value + " - pic");
else Console.WriteLine(m.Value + " - normal");
}
Console.ReadKey();
}
输出
子串1 - normal
子串2 - pic
子串3 - normal
子串4 - pic
子串5 - pic
子串6 - normal
假如对于pic类型,也就是<pic_和>之间的子串,我还要屏蔽掉 \/:?*"<>| 这几个字符,且首尾不能为英文句点符,应该怎么写?
读取的时候识别到<pic_ 图片文件名.后缀名>就根据其中的图片文件名子串到数据库图片表中获取图片,所以 图片文件名中若含有 \/?:*"|<> 等字符都应该识别成非法文件名。
提取所有子串并判断分类的话我就不会写,你写的我也不能完全理解,所以你给出答案后最好稍加解释,呵呵
"<pic_[^\\.]{1}.{1}[^<>\\\\/\\:\\?\\*\"\\|]{1,}[^\\.]{1}>"
{
string s = "很好很强大<pic_1.2.10.8.\\8_title_shapepic.jpg>很黄很暴力<pic_1.jpg>很傻很天真<pic_2.bmp>我爸是李刚<pic_3.jpeg><pic_4.png><pic_5.gif>xxxxxxx<pic_test.>";
foreach (Match m in Regex.Matches(s, @"(?<=<(pic_)|(>|^))(?:[^<>\\/?:*""|]|([\\/?:*""|]))+"))
{
if (m.Groups[1].Success)
{
if (m.Groups[3].Success)
{
Console.WriteLine(m.Value + "\t[pic - error]");
}
else
{
Console.WriteLine(m.Value + "\t[pic]");
}
}
else
{
Console.WriteLine(m.Value + "\t[normal]");
}
}
Console.ReadKey();
}输出很好很强大 [normal]
1.2.10.8.\8_title_shapepic.jpg [pic - error]
很黄很暴力 [normal]
1.jpg [pic]
很傻很天真 [normal]
2.bmp [pic]
我爸是李刚 [normal]
3.jpeg [pic]
4.png [pic]
5.gif [pic]
xxxxxxx [normal]
test. [pic]
非常感谢!(?<=<(pic_)|(>|^))(?:[^<>\\/?:*""|]|([\\/?:*""|]))+顺便再请教下:(?<=<(pic_)|(>|^)) ——这是Groups[1]
(?:[^<>\\/?:*""|]|([\\/?:*""|]))——这是Groups[3]Groups[3]后一个判断条件([\\/?:*""|])有啥用呢?
另外Groups[2]跑哪里去了?
(>|^) Groups[2]
([\\/?:*""|]) Groups[3]其他的自己仔细理解一下吧。
实在不好意思,俺是相当愚钝"(?<=<(pic_)|(>|^))(?:[^<>\\/?:*""|]|([\\/?:*""|]))+"查了不少资料还是不明白,为啥 (?<=<(pic_)|(>|^))不写成 (?<=(<pic_)|(>|^)),以及从<(pic_)和(>|^)这两个零宽断言为啥可以同时捕捉 <pic_和>,以及 >和<,以及>和^,以及&和<之间的所有字符串
?:[^<>\\/?:*""|]|([\\/?:*""|])的意思是不捕捉除了\/?:*"以外的字符,然后又捕捉\/?"*:,这不是自相矛盾耽误你时间不好意思,我愿再补40分
举例有说啊……就是 .号不能在<pic_ 和 >之间的字符串的开头或者结尾
{
string s = "很好很强大<pic_1.2.10.8.\\8_title_shapepic.jpg>很黄很暴力<pic_1.jpg>很傻很天真<pic_2.bmp>我爸是李刚<pic_3.jpeg><pic_4.png><pic_5.gif>xxxxxxx<pic_test.>";
foreach (Match m in Regex.Matches(s, @"(?<=<pic_(?<err>\.)?)(?:[^<>\\/?:*""|]|(?<err>[\\/?:*""|]))+?(?<err>\.)?(?=>)|(?<=>)[^<>]+"))
{
if (m.Groups[1].Success)
{
if (m.Groups["err"].Success)
{
Console.WriteLine(m.Value + "\t[pic - error]");
}
else
{
Console.WriteLine(m.Value + "\t[pic]");
}
}
else
{
Console.WriteLine(m.Value + "\t[normal]");
}
}
Console.ReadKey();
}输出1.2.10.8.\8_title_shapepic.jpg [pic - error]
很黄很暴力 [normal]
1.jpg [normal]
很傻很天真 [normal]
2.bmp [normal]
我爸是李刚 [normal]
3.jpeg [normal]
4.png [normal]
5.gif [normal]
xxxxxxx [normal]
test. [pic - error]
貌似还是有问题,、、
1.开头的“很好很强大” 没有捕捉到
2. 所有正常pic文件全部捕捉到了normal组里
汇报下,改成这样,好像暂时没问题了。
有点困惑的是,把Groups[2]中的(?<=>)改成(?<=(>|^))后,似乎Groups[2]就变成Groups[1]了。
string regular3 = @"(?<=<pic_)(?<err>\.)?(?:[^<>\\/?:*""|]|(?<err>[\\/?:*""|]))+?(?<err>\.)?(?=>)|(?<=(>|^))[^<]+";foreach (Match m in mc)
{
if (m.Groups[1].Success)
{
listView1.Items.Add(m.Value);
listView1.Items[listView1.Items.Count - 1].SubItems.Add("normal");
}
else
{
if (m.Groups["err"].Success)
{
listView1.Items.Add(m.Value);
listView1.Items[listView1.Items.Count - 1].SubItems.Add("error");
}
else
{
listView1.Items.Add(m.Value);
listView1.Items[listView1.Items.Count - 1].SubItems.Add("pic");
}
}
}