Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-20
5
Medium Priority
?
93 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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…

721 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