In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use formal expressions in C#", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "how to use regular expressions in C#" this article.
one。 Check input
One of the most common places for regular expressions is to validate user input using predefined formats (for example, forced rules to ensure that passwords contain specific characters that make it difficult to break). These rules are typically defined as regular expressions. Regular expressions are also often used to verify simple inputs, such as e-mail addresses and phone numbers.
One of the key classes that the .NET Framework provides for manipulating regular expressions is the RegEx class. This class provides a static method IsMatch that returns a Boolean value indicating whether the specified input string matches a given regular expression.
In the following code, a normal regular expression is used to test the validity of the e-mail address:
String emailPattern = @ "^ ([\ w -\.] +) @ (\ [[0-9] {1pr 3}\. [0-9] {1pr 3}\.) | [ccc] (([\ w -] +\.) +) ([a-zA-Z] {2pm 4} | [0-9] {1pm 3}) (\]?) $"; Console.Write ("Enter an e-mail address:") String emailInput = Console.ReadLine (); bool match = Regex.IsMatch (emailInput, emailPattern); if (match) Console.WriteLine ("E-mail address is valid."); else Console.WriteLine ("Supplied input is not a valid e-mail address.")
If you can't figure out this regular expression, don't worry. The basic idea of the e-mail model is that it requires some alphanumeric characters, followed by an @ symbol, then some character combinations, followed by a ".", followed by at least two characters. You can try the previous code with different inputs to see what you get. Even if you don't understand the regular expressions themselves, as long as you know they exist, you can use it in your application to verify the input.
two。 Extract data from input
Another common use of C # regular expressions is to parse text based on expressions and use them to extract data from user input (called group matching).
The regular expression includes a feature called a group. A group allows you to put a named identity in a specific section of the regular expression. When you call Match () to compare input data against a pattern, the result actually divides the match into groups, allowing you to extract the part of the input that matches each group.
For example, in the previous example, we created a username that allows us to extract all data that precedes the @ symbol in an e-mail address. Then, when performing a match, we can extract the username from the input using the named group of regular expressions.
The following code shows how to extract the protocol name and port number from the URL entered by a user on the console. The great thing about formal expressions is that they use their own language; therefore, they don't have to rely on C, C++, C #, VB.NET, or any other language. The regular expression in the following code comes from a MSDN example:
String urlPattern = @ "^ (?\ w +): / / [^ /] +? (?:\ d +)? /"; Console.WriteLine (); Console.Write ("Enter a URL for data parsing:"); string url = Console.ReadLine (); Regex urlExpression = new Regex (urlPattern, RegexOptions.Compiled); Match urlMatch = urlExpression.Match (url); Console.WriteLine ("The Protocol you entered was" + urlMatch.Groups ["proto"] .value) Console.WriteLine ("The Port Number you entered was" + urlMatch.Groups ["port"] .value)
When you run the previous code using URL without a port number, you will notice that you don't get any group values. This is because the input does not match the C # regular expression at all. When there is no match, you obviously cannot extract meaningful data from a given group. When you run the previous code with a URL that matches the port number of the regular expression, you will get the output shown in the following text:
Entera URL for data parsing: http://server.com:2100/home.aspx The Protocol you entered was http The Port Number you entered was: 2100 and above are all the contents of the article "how to use regular expressions in C #". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.