Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

739 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