关于一个正则表达式的问题,在线等,高手进 我现在得到一个网页的源文件代码,只想得到其中的汉字,也就是其中的HTML代码还有样式,或者JS等都不要,请问这个正则如何写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 see:http://www.aspxboy.com/code/default.aspx/S7s2zPJbeI9PJA/OKHpAVwVaUEhf02EgEOgeB3Yd5Z7fLL3ZNgdv7XIqertUOYm9kbQ62LshbJBwvWPBBYa4E/gzyfOfiDJsv4WYTjeRjcanEIHwAhNUjA==.Aspx System.Text.RegularExpressions.Regex.Replace(Encoding.Default.GetString(pageData),"[^u4E00-u9FA5]",""); 操,竟然没有一个能解答这个问题的人,太让我失望了,这是我自己弄得一个答案,经过测试已经成功, using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Text.RegularExpressions;/// <summary>/// 页面数据校验类/// </summary>public static class PageValidate{ private static Regex RegNumber = new Regex("^[0-9]+$"); private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$"); private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$"); private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$ private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); #region 数字字符串检查 /// <summary> /// 检查Request查询字符串的键值,是否是数字,最大长度限制 /// </summary> /// <param name="req">Request</param> /// <param name="inputKey">Request的键值</param> /// <param name="maxLen">最大长度</param> /// <returns>返回Request查询字符串</returns> public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen) { string retVal = string.Empty; if (inputKey != null && inputKey != string.Empty) { retVal = req.QueryString[inputKey]; if (null == retVal) retVal = req.Form[inputKey]; if (null != retVal) { retVal = SqlText(retVal, maxLen); if (!IsNumber(retVal)) retVal = string.Empty; } } if (retVal == null) retVal = string.Empty; return retVal; } /// <summary> /// 是否数字字符串 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsNumber(string inputData) { Match m = RegNumber.Match(inputData); return m.Success; } /// <summary> /// 是否数字字符串 可带正负号 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsNumberSign(string inputData) { Match m = RegNumberSign.Match(inputData); return m.Success; } /// <summary> /// 是否是浮点数 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsDecimal(string inputData) { Match m = RegDecimal.Match(inputData); return m.Success; } /// <summary> /// 是否是浮点数 可带正负号 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsDecimalSign(string inputData) { Match m = RegDecimalSign.Match(inputData); return m.Success; } #endregion #region 中文检测 /// <summary> /// 检测是否有中文字符 /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static bool IsHasCHZN(string inputData) { Match m = RegCHZN.Match(inputData); return m.Success; } #endregion #region 邮件地址 /// <summary> /// 是否是浮点数 可带正负号 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsEmail(string inputData) { Match m = RegEmail.Match(inputData); return m.Success; } #endregion #region 其他 /// <summary> /// 检查字符串最大长度,返回指定长度的串 /// </summary> /// <param name="sqlInput">输入字符串</param> /// <param name="maxLength">最大长度</param> /// <returns></returns> public static string SqlText(string sqlInput, int maxLength) { if (sqlInput != null && sqlInput != string.Empty) { sqlInput = sqlInput.Trim(); if (sqlInput.Length > maxLength)//按最大长度截取字符串 sqlInput = sqlInput.Substring(0, maxLength); } return sqlInput; } /// <summary> /// 字符串编码 /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static string HtmlEncode(string inputData) { return HttpUtility.HtmlEncode(inputData); } /// <summary> /// 设置Label显示Encode的字符串 /// </summary> /// <param name="lbl"></param> /// <param name="txtInput"></param> public static void SetLabel(Label lbl, string txtInput) { lbl.Text = HtmlEncode(txtInput); } public static void SetLabel(Label lbl, object inputObj) { SetLabel(lbl, inputObj.ToString()); } #endregion}第22行 private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); 就是你要的正则式.....这是在这个论坛搜索出来的(说明显然有人会,还不少....),不是我自己写的...就告诉楼主,说话前先注意一下........ 楼主新来的吧,建议问问题前先在这里搜索一下,或者去高手的blog看看先 对CSS苦恼中!!! 伪静态发布IIS后 无法浏览 在线等 求一"以逗号分隔的整形数字串"的正则,末尾无逗号? 》》》100分求一个能够运行的,有注释的Neat Upload应用例子 我们是否都被MVC了 请问ASP.NET如何查看调用的位置? 新手问个小问题,Datalist数据编辑! 怎么让Label、litral 或DIV里的文本可以多行显示???????????????/ VSS,删除文件问题,请大家帮忙~! 求助:字符问题? 用htmlinputfile上载文件 想限定文件的类型 css文件中,用到url()引用图片,可以在运行时改变其路径吗?
see:http://www.aspxboy.com/code/default.aspx/S7s2zPJbeI9PJA/OKHpAVwVaUEhf02EgEOgeB3Yd5Z7fLL3ZNgdv7XIqertUOYm9kbQ62LshbJBwvWPBBYa4E/gzyfOfiDJsv4WYTjeRjcanEIHwAhNUjA==.Aspx
太让我失望了,这是我自己弄得一个答案,经过测试已经成功,
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;/// <summary>
/// 页面数据校验类
/// </summary>
public static class PageValidate
{
private static Regex RegNumber = new Regex("^[0-9]+$");
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); #region 数字字符串检查 /// <summary>
/// 检查Request查询字符串的键值,是否是数字,最大长度限制
/// </summary>
/// <param name="req">Request</param>
/// <param name="inputKey">Request的键值</param>
/// <param name="maxLen">最大长度</param>
/// <returns>返回Request查询字符串</returns>
public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
{
string retVal = string.Empty;
if (inputKey != null && inputKey != string.Empty)
{
retVal = req.QueryString[inputKey];
if (null == retVal)
retVal = req.Form[inputKey];
if (null != retVal)
{
retVal = SqlText(retVal, maxLen);
if (!IsNumber(retVal))
retVal = string.Empty;
}
}
if (retVal == null)
retVal = string.Empty;
return retVal;
}
/// <summary>
/// 是否数字字符串
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumber(string inputData)
{
Match m = RegNumber.Match(inputData);
return m.Success;
}
/// <summary>
/// 是否数字字符串 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumberSign(string inputData)
{
Match m = RegNumberSign.Match(inputData);
return m.Success;
}
/// <summary>
/// 是否是浮点数
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimal(string inputData)
{
Match m = RegDecimal.Match(inputData);
return m.Success;
}
/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimalSign(string inputData)
{
Match m = RegDecimalSign.Match(inputData);
return m.Success;
} #endregion #region 中文检测 /// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
} #endregion #region 邮件地址
/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsEmail(string inputData)
{
Match m = RegEmail.Match(inputData);
return m.Success;
} #endregion #region 其他 /// <summary>
/// 检查字符串最大长度,返回指定长度的串
/// </summary>
/// <param name="sqlInput">输入字符串</param>
/// <param name="maxLength">最大长度</param>
/// <returns></returns>
public static string SqlText(string sqlInput, int maxLength)
{
if (sqlInput != null && sqlInput != string.Empty)
{
sqlInput = sqlInput.Trim();
if (sqlInput.Length > maxLength)//按最大长度截取字符串
sqlInput = sqlInput.Substring(0, maxLength);
}
return sqlInput;
}
/// <summary>
/// 字符串编码
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static string HtmlEncode(string inputData)
{
return HttpUtility.HtmlEncode(inputData);
}
/// <summary>
/// 设置Label显示Encode的字符串
/// </summary>
/// <param name="lbl"></param>
/// <param name="txtInput"></param>
public static void SetLabel(Label lbl, string txtInput)
{
lbl.Text = HtmlEncode(txtInput);
}
public static void SetLabel(Label lbl, object inputObj)
{
SetLabel(lbl, inputObj.ToString());
} #endregion}第22行 private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); 就是你要的正则式.....这是在这个论坛搜索出来的(说明显然有人会,还不少....),不是我自己写的...就告诉楼主,说话前先注意一下........