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
Printable View
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
"[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..
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...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