1. Overview
in the computer room charge system, almost all forms have a text box or combo box, when the user to operate, the first to determine whether it is empty, and then to determine the various properties, such as: Card number, school number, amount, etc. can only enter the number, some information can only enter letters, or set the input of some information to enter only numbers and letters. For example, when we register for a mailbox, there are certain character settings:
The following is the 163 mailbox registration page
When we do the computer room charge system, if using the normal method, directly with the ASCII code to set up, it feels a bit complicated, now we come together to recognize the programmer must have one of the seven skills of "regular expression."
2. What is a regular expression?
First Look at a comic:
A regular expression is a "rule string" that is used to express a filter logic for a string by using a predefined set of specific characters, together with a combination of these specific characters.
For example: You can only enter a number: "^[0-9]*$".
Only Chinese characters can be entered: "^[\u4e00-\u9fa5]{0,}$".
You can only enter a string consisting of 26 English letters: "^[a-za-z]+$".
3. How to use regular expressions?
Let's take a look at how to use it.
Let's take the form of adding a new user as an example:
Public Const C_REGULAR_LENNUM = "^[a-za-z0-9]+$" can only enter characters that consist of a number and 26 English letters public const C_REGULAR_NUMLIMIT = "^\\d {6,16}$ " ' can only enter numbers from 6 to 16 digits.
' <summary> ' detects if the input string matches the regular expression item ' ' </summary> ' ' <param name= ' strpattern ' > Regular expression Items </ Param> ' <param name= ' textreg ' > Input string </param> ' <returns> return result matches </returns> ' ' < ; Remarks><span style= "font-family:kaiti_gb2312;" >2014 August 28 16:02:20</span></remarks> public Function funcheckregular (ByVal Strpattern as String, Textreg as String) Return Regex.IsMatch (Textreg, Strpattern) End function ' <summary> ' ' judgment letter of the specific execution Number, determine whether to enter the number ' </summary> ' ' <param name= ' arraycontrol ' > struct array </param> ' <returns>true Indicates a match, false does not match </returns> ' <remarks>2014-07-09 10:30:24</remarks> public Function checkregular (B Yval Arraycontrol () as term, ByVal Strpattern as String, Warning as String) as a Boolean Dim Termcontrol as term ' Declare a term type variable Termcontrol ' to traverse all elements in the struct array, and if the control text input is not valid, give the corresponding returnThe value for every termcontrol in Arraycontrol ' iterates over all elements in the struct array If TypeOf termcontrol.contr Olsub is TextBox and then ' determines if the control is a text box if TermControl.controlSub.Text.Trim <> "Then ' determines the text box Whether the content is empty, do not empty down if Funcheckregular (Strpattern, termControl.controlSub.Text) = False Then MessageBox.Show (Termcontrol.strtext + "Bar--! + Warning, "friendly Tips", MessageBoxButtons.OK, MessageBoxIcon.Information) termControl.controlSub.Focus () ' Give the focus to the empty control Return False Exit Function End If End If Next Return True End Function
The following code is called at the interface layer (UI):
Public Sub rdimstrnum () ReDim Preserve arraycontrol (0) ' redefine array dimension ' Initialize array arraycontrol (0) = New term ( Txtuserid, "user name") End Sub Public Sub rdimnum () ReDim Preserve Arraycontrol (1) ' redefine array dimension ' Initialize array Arraycontrol (0) = new term (txtuserpwd, "password") Arraycontrol (1) = new Term (txtconfirm, "Confirm password") End Sub
' Determine if user input is legal (numbers and letters) call Rdimstrnum () Dim Strpattern = C_regular_lennum ' Regular expression Dim Warning as String = " You can only enter a character consisting of a number and 26 English letters " if Checkregular (Arraycontrol, Strpattern, Warning) = False then Exit Sub End if ' Determine if user input is legal (number) call rdimnum () Dim pattern = c_regular_numlimit ' Regular expression Dim Warn as String = " Please enter 6 to 16 digits " If checkregular (Arraycontrol, pattern, Warn) = False then Exit Sub End if
3. Concluding remarks
in the daily work of programmers, data processing occupies a considerable proportion. And all of the data in the text also occupy a considerable proportion. Text can be understood, has good transparency, and is conducive to system development, testing and maintenance must have a certain regularity to follow a rule, and the regular expression is exactly what we need, it provides us with powerful string processing capabilities.
Reprint Please specify source:http://blog.csdn.net/zlts000/article/details/38021335
Restrict string input--Regular expression (vb.net)