Solved

Adding conditions to a REgEx pattern

Posted on 2011-03-22
7
654 Views
Last Modified: 2012-06-21
I have some lines of code based on Regular Expressions, and I would like to add some conditions.

1) I would like to test for the string SEAT(S) as well as ROW(S) here:
          .Pattern = "ROW(S)?\s*(\d+)\s*([,-/|]|to|thru|through)\s*(\d+)"
    I tried combining them with parentheses and a pipe, but that doesn't work.

2) I would like to add something here that allows for a space between the number and the letters
          .Pattern = "\d+[A-M]+"

Below is the entire code.

Thanks,
John
Function GetSeats(InputCell As Range) As String   ' 'brettdj & Brad
    Dim RegEx, RegM, RegMC
    Dim MyDic
    Dim tmpStr As String
    Dim i As Long
 
    Set MyDic = CreateObject("scripting.dictionary")
    Set RegEx = CreateObject("vbscript.regexp")
    With RegEx
        '.Pattern = "ROW(S)? (\d+)-(\d+)"
        '.Pattern = "ROW(S)?\s*(\d+)\s*[,-/]\s*?(\d+)" 'Brad
        .Pattern = "ROW(S)?\s*(\d+)\s*([,-/|]|to|thru|through)\s*(\d+)"  'expanded Brad
        .Global = True
        .IgnoreCase = True
        If .test(InputCell.Value) Then
            Set RegMC = .Execute(InputCell)
            For Each RegM In RegMC
                If MyDic.exists(LCase$(RegM)) = False Then
                    For i = RegM.submatches(1) To RegM.submatches(3)
                        Select Case i
                        Case Is <= 4
                            tmpStr = tmpStr & i & "ACDF, "
                        Case Is < 7 > 4
                            tmpStr = tmpStr & i & "ABCDEF, "
                        Case Else
                            tmpStr = tmpStr & i & "ABCDEFHJK, "
                        End Select
                    Next
                    tmpStr = Left$(tmpStr, Len(tmpStr) - 2)
                    MyDic.Add LCase$(RegM), 1
                End If
            Next
        End If
        .Pattern = "\d+[A-M]+"
        If .test(InputCell.Value) Then
            Set RegMC = .Execute(InputCell)
            For Each RegM In RegMC
                If tmpStr = vbNullString Then
                    tmpStr = RegM
                Else
                    tmpStr = tmpStr & ", " & RegM
                End If
            Next
        End If
        GetSeats = tmpStr
    End With
End Function

Open in new window

0
Comment
Question by:gabrielPennyback
  • 3
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 400 total points
ID: 35194497
1) This should work:
          .Pattern = "(?:ROW|SEAT)(S)?\s*(\d+)\s*([,-/|]|to|thru|through)\s*(\d+)"

2)
          .Pattern = "\d+ ?[A-M]+" 'Allow up to one space
          .Pattern = "\d+ *[A-M]+" 'Allow any number of spaces
0
 
LVL 81

Expert Comment

by:byundt
ID: 35194589
I'm not understanding why TerryAtOpus put the ?: at the front of the pattern. From my testing, you'd prefer it like this:
.Pattern = "(ROW|SEAT)(S)?\s*(\d+)\s*([,-/|]|to|thru|through)\s*(\d+)"
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35194604
?: makes it a non-capturing group. The asker would need to alter the group reference numbers further down in the code if they added an extra capturing group.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 81

Assisted Solution

by:byundt
byundt earned 100 total points
ID: 35194613
Note that with the modified pattern, you have an extra parenthetical group. You therefore need to change statement 19 to:
For i = RegM.submatches(2) To RegM.submatches(4)

Open in new window

Brad
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35194616
Exactly...
0
 
LVL 81

Expert Comment

by:byundt
ID: 35194672
TerryAtOpus,
Thanks!

Your syntax wasn't in the web page I use as reference for RegExp syntax, but I see your usage here: http://msdn.microsoft.com/en-us/library/bs2twtah%28v=vs.71%29.aspx

Brad
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 35194804
I don't know where I'd be without you people. Yes I do ... I'd be in the unemployment line! :-)

Thanks,
John
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

809 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