Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand regular expressions in C # and .NET Framework

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to understand regular expressions in C# and .NET Framework". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand regular expressions in C# and .NET Framework.

C # and .NET Framework regular expressions

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.

A key class for manipulating regular expressions provided by C # and the .NET Framework 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.

Another common use of regular expressions is to parse text based on expressions and use them to extract data from user input (called group matching).

Regular expressions include 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 regular 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 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 so far, I believe you have a better understanding of "how to understand regular expressions in C # and .NET Framework". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report