Solved

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

Posted on 2015-01-20
5
89 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 47

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 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 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:Michael74
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

773 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