Solved

Adding conditions to a REgEx pattern

Posted on 2011-03-22
7
653 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Replacing an excel cell that says #value with a specific number 1 24
time format showing wrong 12 50
macro for closing opened workbook 6 19
Access Excel export not behaving 2 25
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
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…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

911 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