这样截取字符串为什么不行?

tongki 2009-05-04 01:05:57
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1"><title>
无标题页
</title></head>
<body>
<form name="form1" method="post" action="left.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJLTM2MjEzOTcxZGQSc/pnM0H7hCweWzr+7ESfLCEn+g==" />

<table width="203" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="656"><table width="203" border="0" cellpadding="0" cellspacing="0">
<tr>
</table>
</form>
</body>
</html>




string rS = "";

string s = 上页的字符串;
rS = Regex.Replace(s, @"<form[^>]*>.*?</form>", "$1", RegexOptions.IgnoreCase);
return rS;

============================================================
想取得<form></form>中间的字符串,可以还rS还是原来的字符串,请帮忙看一下是哪里出了问题?
...全文
232 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
half_bucket 2009-05-05
  • 打赏
  • 举报
回复

Regex reg=new Regex(@"<form(.*?)>(?<value>(\s|.)*?)</form>",RegexOptions.IgnoreCase);
Match m=reg.Match(rs);
if(m.Success)
{
string ret=m.Groups["value"].value.Trim();
}
utopia54 2009-05-05
  • 打赏
  • 举报
回复
this.txtRegex.Text = "fg<form> Own future depend on itself </form> ";
string strPattern2 = @"(?<=<form[^>]*?>).*(?=<\/form>)";
Match match = Regex.Match(strInput, strPattern2);
this.txtRegexValue.Text = match.Value;
lsd123 2009-05-04
  • 打赏
  • 举报
回复
.
蓝海D鱼 2009-05-04
  • 打赏
  • 举报
回复

C# codeRegex reg=newRegex(@"(?<=(<form[^>]*>))(\n|.)*(?=</form>)", RegexOptions.IgnoreCase);
Match m=reg.Match(rs);if(m.Success)
{
ret=m.Value;
}

结果

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJLTM2MjEzOTcxZGQSc/pnM0H7hCweWzr+7ESfLCEn+g==" />

<table width="203" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="656"> <table width="203" border="0" cellpadding="0" cellspacing="0">
<tr>
</table>

asdf4525qd 2009-05-04
  • 打赏
  • 举报
回复
其实我觉得form中间加个div,用div的innerHTML属性取更好
lchh0917 2009-05-04
  • 打赏
  • 举报
回复
学习了~
pricks 2009-05-04
  • 打赏
  • 举报
回复
友情帮顶。
Jave.Lin 2009-05-04
  • 打赏
  • 举报
回复
mark..
Fibona 2009-05-04
  • 打赏
  • 举报
回复
 Regex reg = new Regex(@"<form\s*[^>]*\s*>(\s|.)*</form>", RegexOptions.IgnoreCase);
Match m = reg.Match(rs);
if (m.Success)
{
ret = m.Value;
}
birdinskyliuyuan 2009-05-04
  • 打赏
  • 举报
回复
up
tongki 2009-05-04
  • 打赏
  • 举报
回复
哦,好了,我可以自定义一个html隐藏标记来截取,谢谢各位。我明天结分。
tongki 2009-05-04
  • 打赏
  • 举报
回复
to CPIO:

嗯,Match match = Regex.Match(rS, @" <form[^>]*>(.*) </form>", RegexOptions.Singleline);
if (match != null)
{
rS = match.Groups[1].Value;

}
是得到了我想要的,只是想进一步得到<form>...</form>之中的内容,不包含form标记。
wuqianghappy 2009-05-04
  • 打赏
  • 举报
回复
顶一下!
half_bucket 2009-05-04
  • 打赏
  • 举报
回复
不好意思,高手,献丑了。
half_bucket 2009-05-04
  • 打赏
  • 举报
回复
你把自己框死了,要取得form中间的内容么:
<form(.*?)>(\s|.)*?</form>,你也可以命名内容组,以方便使用。
compleat 2009-05-04
  • 打赏
  • 举报
回复
回帖是一种美德!每天回帖即可获得 10 分可用分!
blestcc 2009-05-04
  • 打赏
  • 举报
回复
正則表達式有問題,寫完要先測試能不能正確匹配。
cpio 2009-05-04
  • 打赏
  • 举报
回复
替换是把<form </form>以及之间的替换掉,而不是返回之间的

Regex.Replace(rS, @"<form[^>]*>.*</form>", "$1", RegexOptions.Singleline);

结果如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1"> <title>
无标题页
</title> </head>
<body>
$1
</body>
</html>

如果要得到之间的内容,可以这样:


Match match = Regex.Match(rS, @"<form[^>]*>(.*)</form>", RegexOptions.Singleline);
if (match != null)
{
rS = match.Groups[1].Value;

}
LemIST 2009-05-04
  • 打赏
  • 举报
回复
rs = Regex.Match(rs, @"<form[^>]*>(.*)</form>", RegexOptions.Singleline | RegexOptions.IgnoreCase).Groups[1].Value;
LemIST 2009-05-04
  • 打赏
  • 举报
回复
rs = Regex.Match(rs, @"<form[^>]*>(.*)</form>", RegexOptions.Singleline).Groups[1].Value;
加载更多回复(4)

111,126

社区成员

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

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

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