Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to modify my search pattern code to stop searching after the first occurrence?

Posted on 2015-01-20
5
Medium Priority
?
94 Views
Last Modified: 2015-01-20
My code works great unless it finds multiple matches.

Function Int_Tkt_TWX(strToSearch)
Set oRegEx = CreateObject("vbscript.regexp")
oRegEx.Global = True
oRegEx.IgnoreCase = True
oRegEx.Pattern = "TWX[0-9]{8,}"
Set RegExMatches = oRegEx.Execute(strToSearch)
If RegExMatches.Count = 1 Then
    Int_Tkt_TWX = RegExMatches.Item(0)
Else
    Int_Tkt_TWX = ""
End If
End Function

Open in new window

0
Comment
Question by:kbay808
5 Comments
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 40561233
If you want set 'Int_Tkt_TWX' to "" when there are no matches only, use this...

If RegExMatches.Count = 0 Then
    Int_Tkt_TWX = ""
Else
    Int_Tkt_TWX = RegExMatches.Item(0)
End If

Open in new window

0
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 40561248
Or perhaps a better option is to test that there is a match....

Function Int_Tkt_TWX(strToSearch)
    Set oRegEx = CreateObject("vbscript.regexp")
    oRegEx.Global = True
    oRegEx.IgnoreCase = True
    oRegEx.Pattern = "TWX[0-9]{8,}"
    If oRegEx.Test(strToSearch) Then
        Set RegExMatches = oRegEx.Execute(strToSearch)
        Int_Tkt_TWX = RegExMatches.Item(0)
    End If
End Function

Open in new window

0
 
LVL 24

Expert Comment

by:mankowitz
ID: 40561250
if you want the regex to stop at the first find, you should set
oRegEx.Global = False
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 40561265
If you only want the first match you can also remove the line
oRegEx.Global = True

Open in new window


as the default is false (or set it implicitly if  you want transparency)
https://msdn.microsoft.com/en-us/library/tdte5kwf(v=vs.84).aspx


Then the count value is guaranteed to 1 or 0

Function Int_Tkt_TWX(strToSearch)
    Set regEx = CreateObject("vbscript.regexp")
   
    With regEx
        .Global = False
        .IgnoreCase = True
        .Pattern = "TWX[0-9]{8,}"
    End With
    
    Set RegExMatches = regEx.Execute(strToSearch)
    
    If RegExMatches.Count = 0 Then
        Int_Tkt_TWX = ""
    Else
        Int_Tkt_TWX = RegExMatches.Item(0)
    End If
End Function

Open in new window


Note: It is always a good idea to use with statements
http://xlvba.fr.yuku.com/topic/318/Using-WithEnd-With-statements#.VL8og0eUdAI
0
 

Author Closing Comment

by:kbay808
ID: 40561290
Awesome!!! Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question