PDA

View Full Version : HTML tag با Regular Expression



tefos666
جمعه 11 دی 1388, 12:00 عصر
سلام دوستان ، کسی میتونه کمک کنه و سورس این تیکه برنامه رو برام بنویسه ؟

یک richtextbox میخوام که توش یکسری tag برای HTML نوشتم میخوام وقتی روی یک دکمه کلیک کردم بره تمام tag هایی که مشکل داره رو درست کنه یا bold کنه ، خودم به این نتیجه رسیدم که با Regular Expressions (http://www.zytrax.com/tech/web/regex.htm) ها بنویسم ولی نتونستم link های زیادی هم پیدا کردم ، ظاهرا بهترینش لینک زیر بود ولی موفق نشدم اینو بنویسم ، لطفا کمک کنید ، شدیدا نیاز دارم

http://regexlib.com/Search.aspx?k=html+tags&c=-1&m=-1&ps=20

اینم کدم




public string sPattern = "<(/)?(a|abbr|acronym|address|applet|area|b|base|basef ont|bdo|big|blockquote|body|br|button|caption|cent er|cite|code|col|colgroup|dd|del|dir|div|dfn|dl|dt |em|fieldset|font|form|frame|frameset|h[1-6]|head|hr|html|i|iframe|img|input|ins|isindex|kbd|l abel|legend|li|link|map|menu|meta|noframes|noscrip t|object|ol|optgroup|option|p|param|pre|q|s|samp|s cript|select|small|span|strike|strong|style|sub|su p|table|tbody|td|textarea|tfoot|th|thead|title|tr| tt|u|ul|var|xmp){1}((\"[^\"]*\"*|[^>])*)*>";
private void RegExChecker()
{
try
{
if (Regex.IsMatch(richTextBox1.Text, sPattern))
{

TxtResult.Text = "\n"+" ";
CheckTag();


}
else
{
TxtResult.Text = "\n" + " ";
DialogResult objResult;
objResult = MessageBox.Show(" ", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (objResult == DialogResult.No)
{
return;
}
else
{

// این قسمت واسه هایلایت کردن تگ هاست و زیاد مهم نیست ، باید کد مورد نظر رو اینجا بنویسم
r.Matches(@"<TAG\b[^>]*>(.*?)</TAG>");
Parse();
richTextBox1.TextChanged += new EventHandler(this.TextChangedEvent);
}

اگر سوال اول رو نتونستید جواب بدید لطفا اینو حل کنید
اصلا میشه line های richtextbox رو توی یه حلفه بزارم دونه دونه tag ها رو با یک regular پیدا کنه و بریزه تو یه مقدار و اونو چک کنم ؟

AliRezaPro
جمعه 11 دی 1388, 12:54 عصر
اینجا ها رو ببینید
Search and Highlight Text in a RichTextBox

http://www.dotnetcurry.com/ShowArticle.aspx?ID=146
Searching function in RichTextBox control

http://www.eggheadcafe.com/community/aspnet/2/10026960/searching-function-in-ric.aspxاین کد را تست کنید
/// <summary>
/// method for searching for specified text in a RichTextBox
/// </summary>
/// <param name="text">text we're looking for</param>
/// <param name="matchCase">match case or not?</param>
/// <param name="rtb">RichTextBox we're searching for</param>
public static void Find(string text, bool matchCase, System.Windows.Forms.RichTextBox rtb)
{
try
{
//variable to hold the start position (start of the found text)
int startPos;

//what kind of search are we doing
StringComparison type;

//determine if it's a match case search or not
type = matchCase == true ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

//look for the search text
startPos = rtb.Text.IndexOf(text, type);

//check the position
if (!(startPos > 0))
{
//text doesn't exist so let the user know
MessageBox.Show("Search text: '" + text + "' could not be found", "Text Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
else
{
//Yureka! Select the found text
rtb.Select(startPos, text.Length);
//scroll to the found text
rtb.ScrollToCaret();
//add focus so the highlighting shows up
rtb.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Search Error");
}
}