Results 1 to 2 of 2

Thread: [02/03] RegEx Regular Expression Help

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2004
    Posts
    59

    [02/03] RegEx Regular Expression Help

    Hi

    If I have the string AAAA000, A's being any alphabetical character and 0's being any numerical character.

    How do I display the alphabetical characters in a messagebox?

    Thanks

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [02/03] RegEx Regular Expression Help

    "[A-Z]" in regex syntax can specify the range of letters A to Z, and "\d" represents a numeric digit 0-9. So putting the two together, you can try "^[A-Z]{4}(?=\d{3})" as your pattern. The "{4}" and "{3}" means to repeat the previous group that many times, so the above should match any 4 letters preceeding any 3 digits, and it should only return the the Letters portion, since the digits are enclosed in (?=..), which just means to match at the end on what is inside, but dont return what is inside in the match result. Below is a sample..
    VB Code:
    1. Dim MyString As String = "ABCD123"
    2.         Dim Regex As New System.Text.RegularExpressions.Regex("^[A-Z]{4}(?=\d{3})", _
    3.                                     System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    4.         Dim MyMatches As System.Text.RegularExpressions.MatchCollection = Regex.Matches(MyString)
    5.         For Each Match As System.Text.RegularExpressions.Match In MyMatches
    6.             MessageBox.Show(Match.Value) 'shows ABCD
    7.         Next
    Another item of note is the "^" at the beginning of the pattern. It is to match the beginning of the string, so if your string can appear anywhere inside of the text then you may want to remove it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width