02-10-12, 09:50 AM
كاتب الموضوع : samerselo
يمكننا استخدام نموذج بحث كنموذج تحقق وذلك بتضمينه ضمن الرموز ^ و $ واستخدام الطريقة IsMatch بدلا من الطريقة Matches فمثلا يتحقق الكود التالي من أن النص يحتوي على خمسة أرقام تمثل رمز منطقة في أمريكا
كود :
pattern = "^\d{5}$"
If Regex.IsMatch(Text, pattern) Then
' It's a string containing five digits.
End If
كود :
pattern = "^(?!00000)\d{5}$"
كود :
pattern = "^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{8,}$"
ويظهر في التحقق من رقم في مجال معطى مشاكل مثيرة وبشكل عام قد تريد استخدام التعابير النظامية للتحقق من الأرقام والتواريخ لأن النوع DateTime يزودك بالطرق Parse و Try-Parse وجميع الأنواع الرقمية أيضا مما يوفر مرونة أكثر ومع ذلك في بعض الحالات تكون التعابير النظامية قابلة للتطبيق حتى في هذه المهمة فمثلا قد تريد استخراج الأرقام والتواريخ الصحيحة من وثيقة أطول والتأكد من أن العدد الصحيح Integer يملك قيمة حتى حد معين تعتبر مشكلة عادية بالطبع
كود :
' Validate an integer in the range of 0 to 9,999; accept leading zeros.
pattern = "^\d{1,4}$"
كود :
' Validate an integer in the range 1 to 9,999; reject leading zeros.
pattern = "^(?!0)\d{1,4}$"
…
' Validate an integer in the range 0 to 9,999; reject leading zeros.
' (Same as previous one, but accept a single zero as a special case.)
pattern = "^(0|(?!0)\d{1,4})$"
…
كود :
pattern = "^(25[0–5]|2[0–4]\d|1\d\d|[1–9]\d|\d)$"
كود :
pattern = "^((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}" _
& "(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)$"
كود :
' Validate an integer number in the range 0 to 65,535; leading zeros are OK.
pattern = "^([1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-4]|\d{1,4})$"
كود :
' Validate an integer in the range -32,768 to 32,767; leading zeros are OK.
pattern = "^(-?[12]\d{4}|-?3[0-1]\d{3}|-?32[0-6]\d{2}|-?327[0-5]\d|" _
& "-?3276[0-7]|-32768|-?\d{1,4})$"
كود :
' Validate a time value in the format hh:mm; the hour number can have a leading zero.
pattern = "^(2[0–3]|[01]\d|\d):[0–5]\d$"
كود :
' If the first digit is even, the second digit must be 0, 4, or 8.
' If the first digit is odd, the second digit must be 2 or 6.
pattern = "^([02468][048]|[13579][26])$"
كود :
' This portion deals with months with 31 days.
Dim p1 As String = "(0?[13578]|10|12)/(3[01]|[012]\d|\d)/\d{2}"
' This portion deals with months with 30 days.
Dim p2 As String = "(0?[469]|11)/(30|[012]\d|\d)/\d{2}"
' This portion deals with February 29 in leap years.
Dim p3 As String = "(0?2)/29/([02468][048]|[13579][26])"
' This portion deals with other days in February.
Dim p4 As String = "(0?2)/(2[0-8]|[01]\d|\d)/\d{2}"
' Put all the patterns together.
pattern = String.Format("^({0}|{1}|{2}|{3})$", p1, p2, p3, p4)
' Check the date.
If Regex.IsMatch(Text, pattern) Then
' Date is valid.
End If
كود :
' This portion deals with months with 31 days.
Const s1 As String = "(0?[13578]|10|12)/(3[01]|[12]\d|0?[1–9])/(\d\d)?\d\d"
' This portion deals with months with 30 days.
Const s2 As String = "(0?[469]|11)/(30|[12]\d|0?[1-9])/(\d\d)?\d\d "
' This portion deals with days 1–28 in February in all years.
Const s3 As String = "(0?2)/(2[0–8]|[01]\d|0?[1–9])/(\d\d)?\d\d"
' This portion deals with February 29 in years divisible by 400.
Const s4 As String = "(0?2)/29/(1600|2000|2400|2800|00)"
' This portion deals with February 29 in noncentury leap years.
Const s5 As String = "(0?2)/29/(\d\d)?(0[48]|[2468][048]|[13579][26])"
' Put all the patterns together.
pattern = String.Format("^({0}|{1}|{2}|{3}|{4})$", s1, s2, s3, s4, s5)