1. Regular basis
1. [] Square brackets indicate that matching can match a single character in any point in square brackets, and square brackets are only allowed to match a single character.
2. | Or represents a choice between two items. It cannot be used with square brackets, but can only be used with small brackets.
3. () brackets represent the beginning and end positions of a subexpression
4. ^ No symbol, if used in square brackets, "^" means a character that does not want to match.
5. . Dot character: Match any single character except line break character \n. To match . , use \. .
6. Locator: Locator allows you to pin regular expressions to the beginning or end of the line, ^ and $ refer to the beginning and end of the string, respectively.
7. Qualifier: Qualifier is used to specify how many times a given component of a regular expression must appear before it can meet the match.
- * indicates that the previous subexpression matches 0 or more times
- ? It means matching the previous subexpression 0 or once
- + means that the previous subexpression matches at least once
- {n} means matching the previous subexpression n times
- {n,m} means matching the previous subexpression from n to m times
8. Escape characters
- \d Match a numeric character. Equivalent to [0-9].
- \D Match a non-numeric character. Equivalent to [^0-9].
- \s Match any whitespace characters, including spaces, tabs, page breaks, etc. Equivalent to [ \f\n\r\t\v].
- \S Match any non-whitespace characters. Equivalent to [^ \f\n\r\t\v].
- \w Match letters, numbers, and underscores. Equivalent to '[A-Za-z0-9_]'.
- \W Match non-letters, numbers, underscores. Equivalent to '[^A-Za-z0-9_]'.
- \n Match a newline
- \r Match a carriage return character
- \b Match a word boundary, that is, the position between the word and the space. Match the beginning or ending part of a word
- \B Match non-word boundary Match the middle part of a word, for example: Match: apt in Chapter, /\Bapt/
2. Commonly used matching characters
Only enter numbers: "^[0-9]*$".
Only n-digit numbers can be entered: "^\d{n}$".
Only numbers with at least n digits can be entered: "^\d{n,}$".
Only numbers in m~n are entered:. "^\d{m,n}$"
Only numbers starting with zero and non-zero are entered: "^(0|[1-9][0-9]*)$".
Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$".
Only positive real numbers with 1 to 3 decimal places can be entered: "^[0-9]+(.[0-9]{1,3})?$".
Only non-zero positive integers can be entered: "^\+?[1-9][0-9]*$".
Only non-zero negative integers can be entered: "^\-[1-9][]0-9"*$.
Only characters with length 3 can be entered: "^.{3}$".
Only strings composed of 26 English letters can be entered: "^[A-Za-z]+$".
Only strings composed of 26 capital English letters can be entered: "^[A-Z]+$".
Only strings composed of 26 lowercase English letters can be entered: "^[a-z]+$".
Only strings composed of numbers and 26 English letters can be entered: "^[A-Za-z0-9]+$".
Only strings composed of numbers, 26 English letters, or underscores can be entered: "^\w+$".
Verify user password: "^[a-zA-Z]\w{5,17}$" The correct format is: start with a letter, length between 6 and 18, and can only contain characters, numbers and underscores.
Verify that it contains characters such as ^%&’,;=?$\": "[^%&’,;=?$\x22]+".
Only enter Chinese characters: "^[\u4e00-\u9fa5]{0,}$"
Verify the email address: "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$".
Verify InternetURL: "^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$".
Verification phone number: "^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"The correct format is: "XXX-XXXXXXX", "XXX-XXXXXXXXXX", "XXX-XXXXXXXXXXX", "XXX-XXXXXXXXXXXX, "XXXXXXXXXXXXXX, and "XXXXXXXXXXXXXXX".
Verify ID number (15 or 18 digits): "^\d{15}|\d{18}$".
The 12 months of verification for one year: "^(0?[1-9]|1[0-2])$" is correct format: "01"~"09" and "1"~"12".
Verify 31 days of one month: "^((0?[1-9])|((1|2)[0-9])|30|31)$"The correct format is; "01"~"09" and "1"~"31"
3. How to use
public static bool Match(string strValue) { bool result = (strValue, @"^-[1-9][0-9]*$"); return result; }
This is all about this article about C#’s use of regular expressions. I hope it will be helpful to everyone's learning and I hope everyone will support me more.