正则表达式,高分征答,内详!散分!

iStringTheory 2002-08-24 09:48:16
问题祥述如下:
-------
1、使用正则表达式怎样将如下代码分成几个字符串,字符串的数目等于n+1,n是代码中<div runat=server id="id1....id(n)"></div>(n个div)的数量;
2、同时要求取得各个div的id;

-------- 示例代码 ----------------------
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#FF6600"> </td>
<td bgcolor="#66CC66"> <div id="d002" runat="server"></div></td>
</tr>
<tr>
<td bgcolor="#0066FF"> <div id="d001" runat="server"></div></td>
<td bgcolor="#FFFF66"> </td>
</tr>
</table>
-------- 代码结束 ----------------------
举例如下:
----
上述代码可以拆分成str1、str2、str3三个字符串:

str1="<table width="80%" border="0" cellspacing="0" cellpadding="0"> <tr><td bgcolor="#FF6600"> </td><td bgcolor="#66CC66"> "

str2="</td></tr><tr><td bgcolor="#0066FF"> "

str3="</td><td bgcolor="#FFFF66"> </td></tr></table>"

说明:
--
这里div数量为2,拆分成3个字符串,将<div runat=server id="id1....id(n)"></div>剔除了出来

要求:
--
要求一个通用的代码,不仅限于代码中有两个div的情况,可以对有任意多个div有效。
好啦,不知我说清楚没有,大家看看例子应该可以明白的。
谢谢各位!恭候阁下的答案!再次感谢!(:
...全文
93 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2002-08-26
  • 打赏
  • 举报
回复
try

Dim sPattern As String = "(<div\s+id=""(d\d+)""[^>]+>[^<]*</div>)"
monkey_zeng 2002-08-26
  • 打赏
  • 举报
回复
还没有搞定??
我来帮你推一下!
plife 2002-08-26
  • 打赏
  • 举报
回复
up!
iStringTheory 2002-08-26
  • 打赏
  • 举报
回复
to superkiller(毛毛雨)
帮我看看这个问题先,解决了高分相赠!
superkiller 2002-08-26
  • 打赏
  • 举报
回复
接分
Inyoureyes 2002-08-26
  • 打赏
  • 举报
回复
关于正则表达式正在学习,主要来接点分:)
iStringTheory 2002-08-26
  • 打赏
  • 举报
回复
D:\dot_net\e-news\index.aspx.vb(71): 需要表达式。
D:\dot_net\e-news\index.aspx.vb(72): 名称“sPattern”未声明。
iStringTheory 2002-08-26
  • 打赏
  • 举报
回复
here is my own code,what's wrong?
还是在d\\d处有错,“需要表达式”,哪里有问题?这个是我根据你的C#代码改的VB代码,有问题么?
---------------------------------------------------------------

Dim s As String = TextBox1.Text
Dim sPattern As String = "(\\<div\\s+id=\"(d\\d+)\"[^\\>]+\\>[^\\<]*\\<\\/div\\>)"
Dim re As Regex = New Regex(sPattern)
Dim aSplitResult As String() = re.Split(s)
Dim i
Response.Write("splitted strings:")
For i = 0 To aSplitResult.Length
Response.Write(i & ":" & aSplitResult(i))
i += 3
Next
Response.Write("IDs:")
For i = 2 To aSplitResult.Length
Response.Write(i & ":" & aSplitResult(i))
i += 3
Next
龙腾九霄 2002-08-26
  • 打赏
  • 举报
回复
gz,up!
saucer 2002-08-26
  • 打赏
  • 举报
回复
that is weird, I am running win2k server (English version) with the final release .NET Framework installed, here is my complete code:

testreg.cs:

using System;
using System.Collections;
using System.Text.RegularExpressions;
class TestPattern
{
public static void Main()
{
String s = "<table width=\"80%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>\n"
+ "<td bgcolor=\"#FF6600\"> </td>\n"
+ "<td bgcolor=\"#66CC66\"> <div id=\"d002\" runat=\"server\"></div></td>\n"
+ "</tr><tr>\n"
+ "<td bgcolor=\"#0066FF\"> <div id=\"d001\" runat=\"server\"></div></td>\n"
+ "<td bgcolor=\"#FFFF66\"> </td>\n"
+ "</tr>\n"
+ "</table>\n";

//String sPattern = @"(\<div\s+id=""(d\d+)""[^\>]+\>[^\<]*\<\/div\>)";
String sPattern = "(\\<div\\s+id=\"(d\\d+)\"[^\\>]+\\>[^\\<]*\\<\\/div\\>)";
Console.WriteLine(sPattern);

Regex re= new Regex(sPattern);

String[] aSplitResult = re.Split(s);

int i;
Console.WriteLine("splitted strings:");
for (i=0; i < aSplitResult.Length; i +=3)
Console.WriteLine(i.ToString()+":"+aSplitResult[i]);

Console.WriteLine("IDs:");
for (i=2; i < aSplitResult.Length; i +=3)
Console.WriteLine(i.ToString()+":"+aSplitResult[i]);

}
}



here is the output:
D:\dotnet>csc testreg.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.


D:\dotnet>testreg
(\<div\s+id="(d\d+)"[^\>]+\>[^\<]*\<\/div\>)
splitted strings:
0:<table width="80%" border="0" cellspacing="0" cellpadding="0"><tr>
<td bgcolor="#FF6600"> </td>
<td bgcolor="#66CC66"> 
3:</td>
</tr><tr>
<td bgcolor="#0066FF"> 
6:</td>
<td bgcolor="#FFFF66"> </td>
</tr>
</table>

IDs:
2:d002
5:d001
iStringTheory 2002-08-26
  • 打赏
  • 举报
回复
在d\\d的第二个“\”出错,错误信息:需要表达式
iStringTheory 2002-08-26
  • 打赏
  • 举报
回复
"(\\<div\\s+id=\"(d\\d+)\"[^\\>]+\\>[^\\<]*\\<\\/div\\>)";
^错误信息:需要表达式
iStringTheory 2002-08-26
  • 打赏
  • 举报
回复
kidding?
why say that?
do u think it's very interesting?!
saucer 2002-08-26
  • 打赏
  • 举报
回复
are you kidding me?

try

String sPattern = "(\\<div\\s+id=\"(d\\d+)\"[^\\>]+\\>[^\\<]*\\<\\/div\\>)";
iStringTheory 2002-08-26
  • 打赏
  • 举报
回复
to saucer(思归):
String sPattern = @"(\<div\s+id=""(d\d+)""[^\>]+\>[^\<]*\<\/div\>)";
在@这里程序报错,说无效字符,我把@移到"里面就好了,但这样就不对了吧?
反正执行结果不对,splitted strings:是0,ids返回空,为何?
bluefish922 2002-08-26
  • 打赏
  • 举报
回复
UP
saucer 2002-08-26
  • 打赏
  • 举报
回复
of course, you can get rid of the first pair of (..), but make sure to change the stop to 2, i.e., i+=2

also, make sure your "s" string is not empty, try


Sub Page_Load (Sender as Object, e as EventArgs)
Dim s As String

s = "<table width=""80%"" border=""0"" cellspacing=""0"" cellpadding=""0"">"
s = s & "<tr>"
s = s & "<td bgcolor=""#FF6600""> ;<div id=""d003"" runat=""server""></div></td>"
s = s & "<td bgcolor=""#66CC66""> <div id=""d002"" runat=""server""></div></td>"
s = s & "</tr>"
s = s & "<tr> "
s = s & "<td bgcolor=""#0066FF""> <div id=""d001"" runat=""server""></div></td>"
s = s & "<td bgcolor=""#FFFF66""> </td>"
s = s & "</tr>"
s = s & "</table>"

Dim sPattern As String = "(<div\s+id=""(d\d+)""[^>]+>[^<]*</div>)"
Response.Write ("Pattern:" & sPattern & "<BR>" & s & "<BR>")
Dim re As Regex = New Regex(sPattern)
Dim aSplitResult As String() = re.Split(s)
Response.Write ("Output Array length:" & aSplitResult.Length & "<BR>")
Dim i
Response.Write("splitted strings:")
For i = 0 To aSplitResult.Length-1 step 3
Response.Write(i & ":" & aSplitResult(i) & "<BR>")
Next
Response.Write("IDs:")
For i = 2 To aSplitResult.Length-1 step 3
Response.Write(i & ":" & aSplitResult(i) & "<BR>")
Next
End Sub
</script>

<form runat="server">
</form>
saucer 2002-08-26
  • 打赏
  • 举报
回复
the reason you need +=3 is because there are two pairs of (..) inside the pattern, the Split method output them after every substring


testreg.cs:

using System;
using System.Collections;
using System.Text.RegularExpressions;
class TestPattern
{
public static void Main()
{

String s = "<table width=\"80%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n"
+ "<tr>\n"
+"<td bgcolor=\"#FF6600\"> ;<div id=\"d003\" runat=\"server\"></div></td>\n"
+ "<td bgcolor=\"#66CC66\"> <div id=\"d002\" runat=\"server\"></div></td>\n"
+"</tr>\n"
+"<tr>\n"
+" <td bgcolor=\"#0066FF\"> <div id=\"d001\" runat=\"server\"></div></td>\n"
+" <td bgcolor=\"#FFFF66\"> </td>\n"
+" </tr>\n"
+"</table>\n";


//String sPattern = @"(\<div\s+id=""(d\d+)""[^\>]+\>[^\<]*\<\/div\>)";
String sPattern = "(\\<div\\s+id=\"(d\\d+)\"[^\\>]+\\>[^\\<]*\\<\\/div\\>)";
Console.WriteLine(sPattern);

Regex re= new Regex(sPattern);

String[] aSplitResult = re.Split(s);

int i;
Console.WriteLine("splitted strings:");
for (i=0; i < aSplitResult.Length; i +=3)
Console.WriteLine(i.ToString()+":"+aSplitResult[i]);

Console.WriteLine("IDs:");
for (i=2; i < aSplitResult.Length; i +=3)
Console.WriteLine(i.ToString()+":"+aSplitResult[i]);

}
}

output:
(\<div\s+id="(d\d+)"[^\>]+\>[^\<]*\<\/div\>)
splitted strings:
0:<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#FF6600"> ;
3:</td>
<td bgcolor="#66CC66"> 
6:</td>
</tr>
<tr>
<td bgcolor="#0066FF"> 
9:</td>
<td bgcolor="#FFFF66"> </td>
</tr>
</table>

IDs:
2:d003
5:d002
8:d001
大健 2002-08-26
  • 打赏
  • 举报
回复
关注
sunruijia 2002-08-26
  • 打赏
  • 举报
回复
学过编译原理吗?用自动机DFA
加载更多回复(13)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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