In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how VB.NET regular expressions simplify the program code, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
1. VB.NET regular expression processing class
The VB.NET regular expression must first introduce the namespace System.Text.RegularExpressions, which contains the following seven classes: Regex, Match, MatchCollection, GroupCollection, CaptureCollection, Group, Capture.
The Regex class represents an immutable (read-only) regular expression class and sets the pattern of the string to be matched. The Match class represents the result of a regular expression matching operation. The MatchCollection class represents a successful non-overlapping matching sequence, that is, the resulting set of strings. These three classes are the ones that are most used in regular expressions.
The common way to use regular expression classes is:
First generate an instance of the Regex class, enter the pattern of the regular expression in the passed-in parameters, and then use the corresponding methods of the Regex class, such as IsMatch (to determine whether it matches), Match (to return the matching string), and so on.
Of course, you can also use the shared method of the Regex class without instantiating the Regex class-- IsMatch, Match, and so on (that is, the shared version of the above method) to get the desired results. This method is suitable for the case that the matching pattern is not fixed and the matching times are small.
Next, an application of regular expression in VB.NET is given.
2. Time parser
The program reads the time length string entered by the user (such as XX hours XX minutes XX seconds), parses it, and * * represents the time length in seconds (XX seconds). The program interface is as follows:
When the user enters the time value in the text box, the point is calculated and the time value in seconds is obtained after the result. The program first reads the time value from the text box, and then looks for the hours, minutes and seconds, where the regular expression is used to parse the time expression. For example, the regular expression of hours can be expressed as "[0-9] + hours". " [0-9] "means that any number between 0 and 9 can be matched, and" + "means that the character can appear one or more times, so" [0-9] + "can match integers of any length (see the corresponding section of MSDN for the syntax of regular expressions). With this string pattern, you can find the hour part of the expression. The total number of seconds is calculated after matching the hours, minutes, and seconds, respectively, and is displayed after the result.
The program includes a regular expression tool class TimeRegex for calculating time, and a Form1 interface class.
The TimeRegex class code is as follows:
ImportsSystem.Text.RegularExpressions PublicClassTimeRegex PrivatehourAsInteger=0 PrivateminuteAsInteger=0 PrivatesecondAsInteger=0 PrivatetotalTimeAsInteger=0 PrivateexpAsString' time expression SubNew () EndSub 'parses the numeric part PrivateFunctiongetTime (ByValtimeKindAsString) AsString DimtimeMatchAsMatch DimtimeAsString' from the time text first parses the timeMatch=Regex.Match (exp,timeKind) IftimeMatch.Success=TrueThen time=timeMatch.Value 'which contains numbers and Chinese, and then parses the numeral ReturnRegex.Match (time) from the resulting text. "[0-9] +"). Value Else Return "0" EndIf EndFunction 'calculates the total time PublicFunctiongetTotalTime (ByVal_expAsString) AsInteger exp=_exp' according to the passed expression to calculate the hours respectively Minutes, seconds Then we get the total time hour=Integer.Parse (getTime ("[0-9] + hours")) minute=Integer.Parse (getTime ("[0-9] + minutes")) second=Integer.Parse (getTime ("[0-9] + seconds")) totalTime=hour*3600+minute*60+second ReturntotalTime EndFunction EndClass Form1 class code as follows: PublicClassForm1 InheritsSystem.Windows.Forms.Form PrivateregexAsNewTimeRegex Windows forms designer generation code omits PrivateSubButton1_Click (ByValsenderAsSystem.Object) ByValeAsSystem.EventArgs) HandlesButton1.Click SecondTime.Text=regex.getTotalTime (timeExp.Text) .ToString & "seconds" EndSub EndClass and above are all the contents of the article "how VB.NET regular expressions simplify program code" 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.