Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adding conditions to a REgEx pattern

Posted on 2011-03-22
7
Medium Priority
?
663 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 1600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 81

Assisted Solution

by:byundt
byundt earned 400 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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 demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

782 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