Solved

Adding conditions to a REgEx pattern

Posted on 2011-03-22
7
652 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 80

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 80

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 80

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

762 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

19 Experts available now in Live!

Get 1:1 Help Now