正则表达式 MatchCollection匹配问题,求助!!

sharpriver 2008-03-27 11:15:30
请教各位大虾:
我想从string chushi 中读取<tr></tr>之间的内容,使用MatchCollection 进行匹配,正则表达式也通过测试,但是为什么程序中老是提示“索引超出了数组界限”这个问题啊?

chushi变量的定义:
string chushi =
RealTimeFxRate1_ExrateDataGrid\" style=\"height:25px;width:100%;background-image:url(../../images/right_content_bg.gif);\">\r\n\t
<tr>\r\n\t\t<td> ·港币->人民币</td><td>90.17</td>\r\n\t</tr>
<tr>\r\n\t\t<td> ·澳元->人民币</td><td>645.82</td>\r\n\t</tr>
<tr>\r\n\t\t<td> ·美元->人民币</td><td>701.60</td>\r\n\t</tr>
<tr>\r\n\t\t<td> ·欧元->人民币</td><\" height=\"32\">\r\n\t\t\t<table cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" align=\"center\">\r\n\t\t\t\t<tr height=\"26\">\r\n\t\t\t\t\t<td width=\"33\"></td>\r\n\t\t\t\t\t<td class=\"white\">外汇实时汇率(中间价)</td>\r\n\t\t\t\t</tr>\r\n\t\t\t

程序:

Regex r = new Regex("<tr>(.*?\n*?)*?</tr>");
MatchCollection mc ;

try
{
mc= r.Matches(chushi);
if(mc.Count != 0)
{
……
}
……

求助~~
...全文
493 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackeyabc 2010-02-28
  • 打赏
  • 举报
回复
我看看是什么,回复太短?????????
sharpriver 2008-03-27
  • 打赏
  • 举报
回复
调试过了,错误在这:
if(mc.Count != 0)
这一句执行不下去了呀,提示“索引超出了数组界限”
应该是mc没有匹配到东西的,但是因为啥不知掉呀
兔子-顾问 2008-03-27
  • 打赏
  • 举报
回复
很显然。错误不在你贴的代码里。debug方式调试,遇到错误就知道哪一行了。
sharpriver 2008-03-27
  • 打赏
  • 举报
回复
刚那个是mc
sharpriver 2008-03-27
  • 打赏
  • 举报
回复
跟踪过了呀:

{System.Text.RegularExpressions.MatchCollection}
System.Object: {System.Text.RegularExpressions.MatchCollection}
_beginning: 0
_done: false
_input: (这个太长,省去)
_length: 696
_matches: {Count=0}
_prevlen: -1
_regex: {System.Text.RegularExpressions.Regex}
_startat: 0
Count: <错误: 发生 {System.IndexOutOfRangeException} 类型的异常>
infinite: 2147483647
IsReadOnly: true
IsSynchronized: false
Item: <无法查看索引属性>
SyncRoot: {System.Text.RegularExpressions.MatchCollection}
wuhq030710914 2008-03-27
  • 打赏
  • 举报
回复

string chushi =" RealTimeFxRate1_ExrateDataGrid\" style=\"height:25px;width:100%;background-image:url(../../images/right_content_bg.gif);\">\r\n\t <tr>\r\n\t\t <td> ·港币->人民币 </td> <td>90.17 </td>\r\n\t </tr> <tr>\r\n\t\t <td> ·澳元->人民币 </td> <td>645.82 </td>\r\n\t </tr> <tr>\r\n\t\t <td> ·美元->人民币 </td> <td>701.60 </td>\r\n\t </tr> <tr>\r\n\t\t <td> ·欧元->人民币 </td> <\" height=\"32\">\r\n\t\t\t <table cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" align=\"center\">\r\n\t\t\t\t <tr height=\"26\">\r\n\t\t\t\t\t <td width=\"33\"> </td>\r\n\t\t\t\t\t <td class=\"white\">外汇实时汇率(中间价) </td>\r\n\t\t\t\t </tr>\r\n\t\t\t";
Regex r = new Regex("<tr>(.*?\n*?)*?</tr>");
MatchCollection mc ;

try
{
mc= r.Matches(chushi);
if(mc.Count != 0)
{
MessageBox.Show("mc");
}
}
catch
{

}


测试过这样是不会报异常的,Count的值也是4
sharpriver 2008-03-27
  • 打赏
  • 举报
回复
if是判断获取的是不是空,如果不是空的话再循环的。
mc.count 这句过不去,提示索引超出了数组界限
因为没有匹配到东西
但是正则表达式即应通过验证了,why?
求救~~
wuhq030710914 2008-03-27
  • 打赏
  • 举报
回复
试试将if(){}改为:

for (int i = 0; i < mc.Count; i++)
{

}
sharpriver 2008-03-27
  • 打赏
  • 举报
回复
LZ检查一下是不是你的chushi 的拷贝或者是其他什么时候增加了一些不必要的空格,用你的代码在我这边是没有报异常的
-----------------------------------
你那里通过了我突然收到启发
我本来用的2003怎么都通不过,刚才试了试2005没有问题
Faint!
2003…………
sharpriver 2008-03-27
  • 打赏
  • 举报
回复
恩。就是你的省略号的部分有问题。你这样不贴完整。尤其把错误隐藏起来。我很怀疑楼主是不是故意灌水。
---------
省略号以下运行不到啊,我调试过了
兔子-顾问 2008-03-27
  • 打赏
  • 举报
回复
恩。就是你的省略号的部分有问题。你这样不贴完整。尤其把错误隐藏起来。我很怀疑楼主是不是故意灌水。
wuhq030710914 2008-03-27
  • 打赏
  • 举报
回复
LZ检查一下是不是你的chushi 的拷贝或者是其他什么时候增加了一些不必要的空格,用你的代码在我这边是没有报异常的
C#字符串和正则表达式参考手册 目 录 第1章 系统处理文本的方式 1 1.1 .NET Framework 1 1.1.1 公共语言运行时 2 1.1.2 .NET Framework类库 3 1.2 文本是一种数据类型 4 1.2.1 C#的数据类型 5 1.2.2 字符和字符集 6 1.2.3 字符串数据类型 10 1.3 文本存储 10 1.3.1 高速缓存技术 12 1.3.2 内置 13 1.3.3 其他方法 14 1.3.4 .NET实现 14 1.4 字符串操作 18 1.4.1 连接字符串 18 1.4.2 从字符串中提取子串 20 1.4.3 比较字符串 20 1.4.4 字符串转换 21 1.4.5 格式化字符串 21 1.5 字符串用法 22 1.5.1 构建字符串 22 1.5.2 分析字符串 24 1.6 国际化 25 1.7 小结 27 第2章 String类和StringBuilder类 28 2.1 学习本章要用到的工具 28 2.2 文本结构 29 2.3 String类 30 2.3.1 内置字符串 32 2.3.2 构建 34 2.3.3 字符串的转义 36 2.4 StringBuilder类 37 2.4.1 长度和容量 39 2.4.2 ToString()方法 41 2.5 字符串操作 42 2.5.1 连接字符串 42 2.5.2 从字符串中提取子串 45 2.5.3 比较字符串 46 2.5.4 格式化 50 2.6 字符串的使用 54 2.6.1 建立字符串 54 2.6.2 标记 58 2.6.3 颠倒字符串次序 61 2.6.4 插入、删除和替换 61 2.7 小结 66 第3章 字符串转换 68 3.1 ToString()方法 68 3.2 把数值表示为字符串 69 3.3 把日期和时间表示为字符串 74 3.4 把其他对象表示为字符串 76 3.5 用字符串表示字符串 78 3.6 把字符串转换为其他类型 79 3.6.1 把字符串转换成数字 79 3.6.2 把字符串转换为日期和时间 82 3.7 在集合与数组之间移动字符串 84 3.7.1 数组 85 3.7.2 ArrayList对象 86 3.7.3 IDictionary对象 88 3.8 小结 88 第4章 国际化 89 4.1 Unicode 89 4.2 .NET Framework的编码类 91 4.3 处理字符串 95 4.3.1 CultureInfo类 96 4.3.2 大写和小写 99 4.3.3 不需要区分文化的操作 101 4.3.4 排序 101 4.4 处理字符 106 4.4.1 关于字符的必要信息 107 4.4.2 代理对 107 4.4.3 组合字符 112 4.5 格式化Unicode字符串 114 4.6 字符串用作资源 115 4.7 小结 119 第5章 正则表达式 120 5.1 System.Text.RegularExpressions命名空间 120 5.2 Regex类 121 5.2.1 RegexOptions枚举 121 5.2.2 类构造函数 122 5.2.3 IsMatch()方法 123 5.2.4 Replace()方法 124 5.2.5 Split()方法 125 5.3 Match类和MatchCollection类 127 5.4 Regex测试器示例 131 5.5 正则表达式基础语法 139 5.5.1 匹配不同类型的字符 139 5.5.2 指定匹配位置 141 5.5.3 指定重复字符 142 5.5.4 指定替换 149 5.5.5 特殊字符 149 5.6 小结 151 第6章 正则表达式的高级概念 152 6.1 分组、替换和反向引用 152 6.1.1 简单的分组 153 6.1.2 Group类和GroupCollection类 156 6.1.3 替换 161 6.1.4 反向引用 162 6.1.5 高级组 163 6.2 在正则表达式中作决策 168 6.3 在正则表达式内设定选项 171 6.4 正则表达式引擎的规则 171 6.5 小结 173 第7章 正则表达式模式 174 7.1 验证字符 174 7.2 验证数字 175 7.2.1 只包含数字 175 7.2.2 只包含整型数 175 7.2.3 只包含浮点数 176 7.3 验证电话号码 177 7.4 验证邮政编码 18
C#制作简单正则表达式测试器部分代码: namespace RegexTest { public partial class Rt : Form { public Rt() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } private void btnPi_Click(object sender, EventArgs e) { if (regText.Text.Trim() != "") { ParameterizedThreadStart ts = new ParameterizedThreadStart(P); Thread th = new Thread(ts); th.Start(th); this.btnPi.Enabled = false; this.process.Visible = true; } else { ParameterizedThreadStart ts = new ParameterizedThreadStart(ShowMsg); Thread th = new Thread(ts); th.Start(th); } } public void ShowMsg(Object obj) { Thread th = (Thread)obj; for (var i = 0; i < 10; i++) { if (i % 2 == 0) { label2.ForeColor = Color.Red; } else { label2.ForeColor = Color.Black; } Thread.Sleep(100); } label2.ForeColor = Color.Red; th.Abort(); } public void P(object o) { Thread t = (Thread)o; resultText.Clear(); String regText = this.regText.Text; String sourceText = this.sourceText.Text; Regex regex = new Regex(regText, RegexOptions.IgnoreCase); MatchCollection matches = regex.Matches(sourceText); this.resultText.Text += "匹配结果数:" + matches.Count + "\n"; this.resultText.Text += "-----------------------------------------------------\n"; foreach (Match match in matches) { this.resultText.Text += "匹配值:" + match.Value + "\n"; this.resultText.Text += "匹配组结果:\n"; foreach (String g in regex.GetGroupNames()) { this.resultText.Text += "组(" + g + "):" + match.Groups[g].Value + "\n"; } resultText.Text += "-----------------------------------------------------\n"; } this.process.Visible = false; this.btnPi.Enabled = true; t.Abort(); } private void btnReplace_Click(object sender, EventArgs e) { resultText.Clear(); String regText = this.regText.Text; String replaceText = this.replaceText.Text; String sourceText = this.sourceText.Text; Regex regex = new Regex(regText); this.resultText.Text += "替换结果:\n"; this.resultText.Text += "-----------------------------------------------------\n"; this.resultText.Text += regex.Replace(sourceText,replaceText); } private void 复制ToolStripMenuItem_Click(object sender, EventArgs e) { if (this.sourceText.SelectedText.Length > 0) { Clipboard.SetText(this.sourceText.SelectedText); MessageBox.Show("已成功将选中文本复制到剪切板"); } else { Clipboard.SetText(this.sourceText.Text); MessageBox.Show("已成功将文本复制到剪切板"); } } private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e) { this.sourceText.Paste(DataFormats.GetFormat("text")); } private void 清空ToolStripMenuItem_Click(object sender, EventArgs e) { this.sourceText.Clear(); } private void toolStripMenuItem1_Click(object sender, EventArgs e) { if (resultText.SelectedText.Length > 0) { Clipboard.SetText(this.resultText.SelectedText); MessageBox.Show("已成功将选中文本复制到剪切板"); } else { Clipboard.SetText(this.resultText.Text); MessageBox.Show("已成功将结果复制到剪切板"); } } private void toolStripMenuItem3_Click(object sender, EventArgs e) { this.resultText.Clear(); } private void Form1_Load(object sender, EventArgs e) { } private void resultText_TextChanged(object sender, EventArgs e) { //this.resultText. } } }

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧