Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now