PDA

View Full Version : آموزش: برخی از الگوهای مورد استفاده در RegEx



abasfar
یک شنبه 17 بهمن 1389, 22:58 عصر
سلام من یک تاپیک سر جمع برای regex ندیدم گفتم من شروع کنم شاید شما هم ادامه بدید




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{class ExtensionSamples
{public static bool IsWholeNumber(this string strNumber)
{if (strNumber == "")return false;
Regex objNotWholePattern = new Regex("[^0-9]");return !objNotWholePattern.IsMatch(strNumber);
}public static bool IsDouble(this string strNumber)
{if (strNumber == "")return false;try{
Convert.ToDouble(strNumber);
}catch (Exception)
{return false;
}return true;
}public static bool IsAlphaNumeric(this string strToCheck)
{bool valid = true;if (strToCheck == "")return false;
Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
valid = !objAlphaNumericPattern.IsMatch(strToCheck);return valid;
}public static bool IsValidAlphaNumericWithSpace(this string strToCheck)
{bool valid = true;if (strToCheck == "")return false;
Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9\\s]");
valid = !objAlphaNumericPattern.IsMatch(strToCheck);return valid;

}public static bool IsValidAlphabetWithSpace(this string strToCheck)
{bool valid = true;if (strToCheck == "")return false;
Regex objAlphaNumericPattern = new Regex("[^a-zA-Z\\s]");
valid = !objAlphaNumericPattern.IsMatch(strToCheck);return valid;
}public static bool IsValidAlphabetWithHyphen(this string strToCheck)
{bool valid = true;if (strToCheck == "")return false;
Regex objAlphaNumericPattern = new Regex("[^a-zA-Z\\-]");
valid = !objAlphaNumericPattern.IsMatch(strToCheck);return valid;
}public static bool IsAlpha(this string strToCheck)
{bool valid = true;if (strToCheck == "")return false;

Regex objAlphaPattern = new Regex("[^a-zA-Z]");
valid = !objAlphaPattern.IsMatch(strToCheck);return valid;
}public static bool IsNumber(this string strNumber)
{try{
Convert.ToDouble(strNumber);return true;
}catch{return false;

}

}public static bool IsInteger(this string strInteger)
{try{
Convert.ToInt32(strInteger);return true;
}catch{return false;
}

}public static bool IsDateTime(this string strDateTime)
{try{

Convert.ToDateTime(strDateTime);return true;
}catch{return false;
}
}public static bool isEmail(this string inputEmail)
{if (inputEmail != null && inputEmail != "")
{string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

Regex re = new Regex(strRegex);if (re.IsMatch(inputEmail))return (true);elsereturn (false);

}elsereturn (false);

}

}
}



ترسیدم جریمه ام کنن ویرایش کردم




شماره موبایل در این فرمت باید باشه : +989111111111
به جای یک ها هر عددی میتواند باشد ...به جز دوتا یک اولی که از 1تا 9 میتوانند باشند ...





System.Text.RegularExpressions.Regex mobileReg =
new System.Text.RegularExpressions.Regex("^\\+989[1-9]{2}\\d{7}\\Z");


موبایل به صورت نرمال



public bool CheckMobileNumber(string MobileNumber)
{
return System.Text.RegularExpressions.Regex.IsMatch(Mobil eNumber, "(^(09|9)[13][0-9]\\d{7}$)");
}

tabrize
یک شنبه 04 اردیبهشت 1390, 12:19 عصر
سلام خوبید ممنون از اطلاعاتی که گزاشتین
این اولین مشکل من بود که در این سایت حل شد
بازم ممنون:قلب::تشویق:

niyaz4872
یک شنبه 04 اردیبهشت 1390, 13:46 عصر
سلام و خسته نباشيد. ميشه توضيح بدهيد RegEx اصلا چي هست.
كاربوردش در چه مواردي هست.