Working with regular expressions in .net

xiaoxiao2021-03-06  39

What are regular expressions? If you have ever used wildcards before eg when trying to find a file on your computer then unwittingly you have used regular expressions. I like to think that regular expressions simply allow you to manipulate strings easier. Regular expressions consist of a lot more than just wildcards though;. we will look at more complex things in a minute Why would I want to use them Regular expressions are extremely useful when you want to extract useful information from a load of junk Say someone wrote a sentence similar?. this: I told Bob that I would text him on his mobile phone His number is 07589123654 and my number is 07891264853. and you need to extract all phone numbers from a sentence similar to that If you have not met regular expressions before you.. would automatically think its either not possible or is There ar very complicated and would involve lots of Mid () 's and Int32.Parse's So how would I go about using them? First you will need to know the basics. E Two Things That You Will Need To Use Regular Expressions IN .NET and Those Are:

An Input string (in our case the string above about bob) A search pattern (the things that tells the regular expressions class what to look for) The only tricky bit is the search pattern, they can sometimes become quite complex (depending on what your trying to extract). to build up a search pattern we will have to do some research first. Finding literal content This is the easiest thing you will learn, simply because you already know it! to find literal content you just write the literal content into the search pattern and that's it. Finding numbers Finding numbers is a little trickier but it's easy once you know how. Say our input string was "My phone number begins with a 0 and ends in a 8". We would use the following search pattern : "[0-9]" This Would Return the Following: 08 Characters Wrapped Around [] 's INDICATE A RANGE. This Could Be from Az Which Would Be [AZ], IT Could Be c to f Which Would Be [CF] Or in Our Case 0 To 9 Which IS [0-9]. Important: Ranges Are Case Sensitive ... Ie [az] would only return lower case letters and [AZ] would only return upper case letters This approach works perfectly if its just a single character but to allow multiple characters we need to use one more method. Say our input string was "My Hotel room is 1045a.

IT WILL NOT RETURN THEMILAR TO THID TO Use Something Similar To this for Our search Pattern: "[0-9] {4}" An Integer That's Wrapped IN {} 's Specifies The Length of the Range. TO EXPLAIN More We Will Use An Example: Input String: "Search Pattern:" [0-9] "RETURns: 1045 Input String:" Search Pattern: "[0 -9] {4} "Returns:. 1045 I hope that makes it a bit clearer Putting it all together Now going back to extracting the phone numbers from the sentence at the start of the article, we will need to combine all of the preceding techniques to extract the phone numbers. All valid UK Mobile Phone numbers begin with '07', so we need to make sure that all the matches returned begin with '07'. '07' is literal content so our Search pattern so far is simply "07" Valid UK Mobile Phone NumBers Are 11 NumBers Long (Including the '07'). So Now We need to make Sure That the matches have 9 NumBers Following the '07'. This Means Our search string will be "07 [0-9] {9}". Sure enough this extracts all Valid UK Mobile Phone numbers. Now we will discuss how to via .NET use Regex (Regular expressions). Accessing Regular Expressions in NET Regular expressions are used in .NET via the System.Text.RegularExpressions Namespace.> Create a new console application and import the System.Text.RegularExpressions by adding "Imports System.Text.RegularExpressions" to the top of the module. Results from our search are Returned Into a 'Matchcollection'. from this 'Matchcollection' We can loop through and retrieve Individual matches. Let's get out! Source Code:

Sub Main () 'Make a new Regex provider Dim objRegex As Regex' This object is what our matches will be put into Dim objMatches As MatchCollection 'This object is used to referance to an individual match Dim m As Match' This string will be our search pattern Dim strSearchPattern As String 'This string will be our input string Dim strInput As String' Set the input string strInput = "I told Bob that I would text him on his mobile phone." & _ "His number is 07589123654 and my number is 07891264853. " 'Set the search pattern strSearchPattern =" 07 [0-9] {9} "' Tell Regex our search pattern objRegex = New Regex (strSearchPattern) 'Fill the matches full of valid phone numbers objMatches = objRegex.Matches (strInput ) 'Loop through the phone numbers that were found For Each m In objMatches' Write the valid phone number onto the console Console.WriteLine (m.Value.ToString & ControlChars.CrLf) Next Console.ReadLine () End Sub that's the basics of regular expressions, there is a lot more to learn about though. I have only scratched the surface of the syntax etc. If you search google for "Regular Expressions" then you will find a lot more info then I have provided. I will Also Post A Sample App That Demonstrates Regular Expressions to Help You Some More. Hope You Have Enjoyed It, Martin.This Article Was Written On 3/19/2004 3:40:00 PM by Martin

转载请注明原文地址:https://www.9cbs.com/read-118339.html

New Post(0)