In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use VB.NET regular expressions, 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. Introduction
In general programming, string manipulation is an indispensable part. For example, we often need to find a specific part of a string, or replace, delete, and so on. Using the traditional static string operation method, we can only find the fixed part of the string, which lacks flexibility. Using VB.NET regular expression, you can specify the pattern of the string to be found and find the same type of string. For example, to decompose the user name and server name in a string in EMAIL format (such as lzcarl@126.com), you can define the following regular expression (? [^ @] +) @ (?. +), and then use the corresponding parser to parse, resulting in two grouping user group and host group. The user name lzcarl is stored in the user group. The server name 126.com is stored in the host group, and this decomposition is difficult to achieve with the traditional method.
Thus it can be seen that as a way of string processing, the flexibility and power of regular expressions can not be compared with the traditional string processing methods.
2. VB.NET regular expression processing class
Using regular expressions in VB.NET requires the introduction of the namespace System.Text.RegularExpressions, which contains the following seven classes: Regex, Match, MatchCollection, GroupCollection, CaptureCollection, Group, and 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 or not
Match), Match (returns 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 VB.NET regular expression is given.
3. Time parsing program
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).
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:
Imports System.Text.RegularExpressions
Public Class TimeRegex
Private hour As Integer = 0
Private minute As Integer = 0
Private second As Integer = 0
Private totalTime As Integer = 0
Private exp As String's time expression
Sub New ()
End Sub
'parse the numeric part according to the time text
Private Function getTime (ByVal
TimeKind As String) As String
Dim timeMatch As Match
Dim time As String
'first parse the parts that contain numbers and Chinese
TimeMatch = Regex.Match (exp, timeKind)
If timeMatch.Success = True Then
Time = timeMatch.Value
Then parse the numbers from the resulting text
Return Regex.Match (time, "[0-9] +") .Value
Else
Return "0"
End If
End Function
'calculate the total time based on the incoming expression
Public Function getTotalTime (ByVal
_ exp As String) As Integer
Exp = _ exp
Calculate the hours, minutes, seconds respectively, and then 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
Return totalTime
End Function
End Class
The Form1 class code is as follows:
Public Class Form1
Inherits System.Windows.Forms.Form
Private regex As New TimeRegex
Windows forms designer generated code omitted
Private Sub Button1_Click
(ByVal sender As System.Object
ByVal e As System.EventArgs)
Handles Button1.Click
SecondTime.Text = regex.
GetTotalTime
(timeExp.Text). ToString & "seconds"
End Sub
End Class
The user inputs 1 hour, 1 minute and 1 second later to calculate, and the result is 3661 seconds, which proves that the result is correct.
The above is all the content of the article "how to use VB.NET regular expressions". 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.