|
-
May 11th, 2006, 09:55 AM
#1
Thread Starter
Member
[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
-
May 11th, 2006, 03:57 PM
#2
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:
Dim MyString As String = "ABCD123"
Dim Regex As New System.Text.RegularExpressions.Regex("^[A-Z]{4}(?=\d{3})", _
System.Text.RegularExpressions.RegexOptions.IgnoreCase)
Dim MyMatches As System.Text.RegularExpressions.MatchCollection = Regex.Matches(MyString)
For Each Match As System.Text.RegularExpressions.Match In MyMatches
MessageBox.Show(Match.Value) 'shows ABCD
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|