Regular expression

xiaoxiao2021-03-06  40

Regular Expression is a text mode composed of normal characters (such as characters a to z) and special characters (called metammatics). This mode describes one or more strings to be matched when the text body is looking for. Regular expression As a template, a character mode matches the search string.

Using regular expressions, you can:

1. Test a pattern of strings. For example, an input string can be tested to see if the string exists or a credit card number mode. This is called data validity verification. 2. Replace the text. You can use a regular expression in the document to identify a particular text, then you can delete it, or replace it with another text. 3. Extract a sub-string from the string based on the mode match. Can be used to find a specific text in the text or input field.

For example, if you need to search the entire Web site to delete some excessive materials and replace some HTML formatted tags, you can use the regular expression to test each file, see if there is a material or HTML you want to find in this file. Formatted tag. With this method, you can narrow the affected file range to those files that contain materials to be deleted or changed. You can then use the regular expression to delete the outdated material, and finally, you can use the regular expression again to find and replace those markers that need to be replaced. Another example explaining the regular expression is a language that is not known for its string processing capabilities. Vbscript is a subset of Visual Basic, with rich string processing features. Visual Basic Scripting Edition similar to c does not have this capability. Regular expression gives a significant improvement in string processing capabilities for Visual Basic Scripting Edition. However, it is possible to still use the regular expression in VBScript, which allows multiple string operations to be executed in a single expression. It is because of the powerful function of "Regular Expression", which makes Microsoft slowly transplant the regular expression object to the window system. Special characters and sequences are used when writing regular expressions. The following table describes the characters and sequences that can be used, and examples are given. Character Description: /: Tag the next character as a special character or a literal value. For example, "N" matches the character "n". "/ n" matches the laundry. Sequence "//" with "/" match, "/ (" matches ".

^: Match the start position of the input. $: Match the end of the input. *: Match the previous character zero or several times. For example, "ZO *" can match "Z", "ZOO". : Match the previous character once or more. For example, "ZO " can match "ZOO" but do not match "Z". ?: Match the previous character zero or once. For example, "a? Ve?" Can match "VE" in "Never". .: Match any character other than changing line characters. (Pattern) matches the pattern and remember to match. Matching subtrips can be obtained from Item [0] ... [N] from the matches collection as a result. If you want to match parentheses characters (and), you can use "/ (" or "/)." X | Y: Match X or Y. For example, "z | food" can match "Z" or "Food". "(z | f) OOD" matches "ZOO" or "Food". {n}: n is a non-negative integer. The match is just n times. For example, "O {2}" cannot match "O" in BOB, but can match the first two O "fooOOD". {N,}: n is a non-negative integer. Match at least n times. For example, "o {2,}" does not match "O" in "Bob", but matches all O. "O {1,}" in "fooOOD" is equivalent to "o ". "O {0,} "Equivalent to" o * ". {N, m}: m and n are non-negative integers. Match at least n times, up to M times. For example," O {1, 3} "matches" foooood "in the top three O. "o {0,1}" is equivalent to "o?". [XYZ]: A character set. Match one of the characters in parentheses. For example, "[ABC]" matches "plain" "a". [^ xyz]: A negative character set. Matching any character in this parentheses. For example, "[^ ABC]" can match "P" in "PLAIN". [AZ]: Represents a certain The characters within the range. Match with any character in the specified interval. For example, "[AZ]" matches "A" and "Z" any lowercase alphabetic characters. [^ MZ]: Negative character interval. Can't The character match in the specified interval. For example, "[MZ]" matches any character that is not in "M" to "Z". / B: Match the boundary of the word, that is, the position between the words and spaces. For example, "ER / B" matches "er" in "Never", but does not match "ER" in "Verb". / B: Matching the non word boundary. "EA * R / B" and "Never Early" "EAR" match. / D: Match with a numeric character. Equivalent to [0-9]. / D: Matching non-digital characters. Equivalent to [^ 0-9]. / F: Paging Match. / N: Match with the wrap character. / R: Match with the carriage return character.

/ S: Match with any white character, including spaces, tabs, pagins, etc. Equivalent "[/ f / n / r / t / v]". / S: Match with any non-blank character. Equivalent to "[^ / f / N / R / T / V]". / T: Matching the player. / V: match the vertical tab. / W: Match with any word character, including underscore. Equivalent to "[A-ZA-Z0-9_]". / W: Match with any non-word character. Equivalent to "[^ a-za-z0-9_]". / NUM: Match NUM, where NUM is a positive integer. Quote Back to remember the match. For example, "(.) / 1" matches two consecutive identical characters. / N: Match N, where N is an octaves. The octal modulation value must be 1, 2 or 3 numbers long. For example, "/ 11" and "/ 011" matches a tab. "/ 0011" equivalent to "/ 001" and "1". The octal exchange value must not exceed 256. Otherwise, only the first two characters are considered part of the expression. Allows the use of the ASCII code in the regular expression. / xn: Match N, where N is a hexadecimal modified value. The hexadecimal modified value must be long for two numbers. For example, "/ x41" matches "A". "/ x041" equivalent to "/ x04" and "1". Allows the use of the ASCII code in the regular expression. RegularExpressionValidator has two main properties to verify validity. ControlTovAlidate contains a value verified. Return the value in the text box. Such as ControlTovAlidate = "TextBox1" ValidationExpression includes a regular expression for verification. Ok, I have the above narrative, we will give an example to explain the regular expression. For example, we want to check the email entered by the user, then what kind of data is a legal email? I can enter this: Test@yesky.com, of course I will also enter: *** @ yyy.com.cn, but such input is illegal: *** @@ com.cn or @ ***. Com.cn, etc., so we derive a legal email address should at least meet the following conditions: 1. Must contain one and only one symbol "@" 2. The first character must not be "@" or " "3." @. "Or. @ 4. The end is not allowed to be character" @ "or". "

So according to the above principles and the syntax in the above table, we can easily get the needs template as follows: "=" ^ / w ((- / w ) | (/ w )) * / @ [A- ZA-Z0-9] (/. | -) [A-ZA-Z0-9] ) * /. [A-ZA-Z0-9] $ "

转载请注明原文地址:https://www.9cbs.com/read-54038.html

New Post(0)