This article is a mirror article of machine translation, please click here to jump to the original article.

View: 27285|Reply: 0

[Source] C# determines if a string is only made up of numbers and letters (Regex.IsMatch)

[Copy link]
Posted on 11/2/2017 9:38:53 PM | | |

Purpose: To determine whether a string is only composed of numbers and letters, to prevent "garbled characters", and to judge the length of the string;
Main function function: System.Text.RegularExpressions.Regex.IsMatch(strMessage, pattern)
strMessage:要判断的字符串;
pattern:判断的条件;
Regular expression uses: 1. Test patterns within strings. 2. Replace text. 3. Extract substrings from strings based on pattern matching.
Regular expression syntax: https://msdn.microsoft.com/zh-cn/library/ae5bf541 (VS.80) .aspx
Here is an analysis of some simple syntax I currently use:
string pattern = @"^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$";
Understand: 1. ^[a-zA-Z0-9]: indicates that it begins with a case or a number;
2. /d{2}: Indicates two numbers
3. {a-zA-Z0-9}: Indicates two numbers followed by a case or number
4. (-\d{3}){2}: Indicates a series of repeated -plus three number formats, such as: -333-444, -123-456
5. [A-Za-z0-9]$: Indicates that it ends in upper and lower case or number;
Correct form: 1111-111-1111;


Understand:
Each character in the entire string must be case-case or numeric;





Previous:One of the comparisons between CMMI and Agile: the essential difference between the two
Next:VS2010 C# Close Port Tool source code
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com