1

I am trying to use Regex in VBA to match a whole word containing a hyphen and numbers. I know the \b would not work because it only set alphabetic boundaries.

Can this be done in Regex in VBA?

rx.Pattern = "[a-z][a-z][0-9]-[0-9][0-9]" 

EDIT: I am sorry if I wasn't clear enough. My pattern has the following format "AA2-11". I want to match that whole string, that is why I can't use rx.pattern = "[a-z][a-z][0-9]-[0-9][0-9]" because that will hit a match if you have for example "AA2-11-4", while I just want "AA2-11"

7
  • 1
    How to Use Regular Expressions in Visual Basic Commented Nov 17, 2013 at 15:04
  • Will the hyphen come first or the number? Commented Nov 17, 2013 at 15:05
  • 1
    Can you please include some of the words in your question ? Commented Nov 17, 2013 at 15:10
  • I am not sure what exactly are you asking. I had to re-read it many times but every time I understood something else so I had to delete my answer. Can you please give some examples as @Sniffer requested? Commented Nov 17, 2013 at 15:45
  • I am sorry if I wasn't clear enough. My pattern has the following format "AA2-11". I want to match that whole string, that is why I can't use rx.pattern = "[a-z][a-z][0-9]-[0-9][0-9]" because that will hit a match if you have for example "AA2-11-4", while I just want "AA2-11". Commented Nov 17, 2013 at 15:49

1 Answer 1

3

Is this what you want?

MORE FOLLOWUP

Thanks for updating your answer, however I am afraid that it doesn't work. Let's say my document has for example AA12-12,AB14-26. The first occurence is not matched.

Sub Sample() Dim regString As String Dim myRegExp As RegExp regString = "AA12-12#AB14-26" '<~~ Matches 'regString = "#AA12-12,AB14-26#" '<~~ Matches 'regString = "AA2-11 is a sample string" '<~~ Matches 'regString = "This is a sample AA2-11-11" '<~~ Doesn't Match 'regString = "This is a sample AA2-11-11 string" '<~~ Doesn't Match 'regString = "This is a sample AA2-11-11 string" '<~~ Doesn't Match regString = " " & regString & " " Set myRegExp = New RegExp With myRegExp .Global = True .Pattern = "\b[a-zA-Z]{2}\d{1,}-\d{2,}\b(?=[^-])" If myRegExp.Test(regString) Then Debug.Print "Found" Else Debug.Print "Not Found" End If End With End Sub 

Or like this

Sub Sample() Dim regString As String Dim myRegExp As RegExp Dim myMatches As MatchCollection Dim myMatch As Match regString = "AA12-12,AB14-26" '<~~ Matches 'regString = "#AA12-12,AB14-26#" '<~~ Matches 'regString = "AA2-11 is a sample string" '<~~ Matches 'regString = "This is a sample AA2-11-11" '<~~ Doesn't Match 'regString = "This is a sample AA2-11-11 string" '<~~ Doesn't Match 'regString = "This is a sample AA2-11-11 string" '<~~ Doesn't Match regString = " " & regString & " " Set myRegExp = New RegExp With myRegExp .Global = True .Pattern = "\b[a-zA-Z]{2}\d{1,}-\d{2,}\b(?=[^-])" Set myMatches = myRegExp.Execute(regString) For Each myMatch In myMatches Debug.Print myMatch.Value Next End With End Sub 
Sign up to request clarification or add additional context in comments.

7 Comments

The second procure is what I was looking for. Thanks much!
Both are same. The only difference is the .IgnoreCase = True :)
Hmmm, I just noticed that the Pattern you have suggested would not work if you have a long string. See in my case I am assigning regString to the content of a word document, regString = ActiveDocument.Range.Txt , I suspect because the caret and the sigil would not work in this case.
@Jeanno: Updated the answer. Let's see If I can come up with something better
Thanks for updating your answer, however I am afraid that it doesn't work. Let's say my document has for example AA12-12,AB14-26. The first occurence is not matched. (Please note I am using For Each mtch In myRegExp.Execute(regString) ) . I think my problem is similar to this question stackoverflow.com/questions/4721982/… but I have hyphens instead of dots, but I can't an error when I try to use the pattern suggested
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.