Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 29th, 2007, 12:02 AM
Registered User
 
Join Date: Jan 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default use regex to split string store in dictionary

hello,
have a vbscript to read file names. strip off the file extension. and parse the file name into two parts. if the name is abc1 and the next is abc2 the basename is abc the strNumber(0) = 1 strNumber(1) = 2

where I'm having trouble is determining the regex to work with unusual names like abc, abc-1, acb-2 and the odd case: 1,2,3

there are no formal file naming conventions. individual users can make up any name or series of names. for a series there is often an identifiable pattern. which can be determined by looking at the names. so I attempt to have the computer "look" at the names. ignore non series names.
here is a sample list of some problem names.
1
2
2bases2
2bases
2battle
2hotel
3
4
5
6
escape2_a
escape2_b
escape2_c
escape2_d
escape2_e
op4two1
op4two2
Prisoned2a
Prisoned2b
bh1_l1.bsp
Bh1_l1_bonus.bsp
bh1_l2.bsp
sc_deadthings.bsp
sc_deadthings2.bsp
sc_deadthings3.bsp
sc_tombofdeath.bsp
sc_tombofdeath-2.bsp
sc_tombofdeath-3.bsp
sc_tombofdeath-4.bsp
sc_tombofdeath-5.bsp

read the directory of file names into an array. strip ".bsp"
Code:
Sub FillArray(objCurrentFolder, objLogFile, arrTestNames)

    Dim strTemp
    Dim strSearch
    Dim strOutput
    Dim objNewFolder
    Dim objFile
    Dim objStream
    dim a
    dim intIndex

    strSearch = ".bsp"

       intIndex = 0
       For Each objFile In objCurrentFolder.Files
           strTemp = Right(objFile.Name, 4)
                If UCase(strTemp) = UCase(strSearch) Then
                    'Got one
                   a=Split(CStr(objFile.Name),".")
                   'populate database
                   arrTestNames (intIndex) = a(0)
                   'strOutput = arrTestNames (intIndex)
                   'objLogFile.writeline strOutput
                   intIndex = intIndex + 1
                   End If
       Next

End Sub
create the dictionary
Code:
Const TextCompare = 1
'Start by making a dictionary of all the base names. The value will be an array of the numerals
Set dicBaseNames = CreateObject("Scripting.Dictionary")
dicBaseNames.CompareMode = TextCompare
For Each strName In arrTestNames
    strBase = GetBaseName(strName)
    strNumber = GetSequenceNumber(strName)
    If dicBaseNames.Exists(strBase) Then
        nUBound = UBound(dicBaseNames(strBase)) + 1
        arrTemp = dicBaseNames(strBase)
        ReDim Preserve arrTemp(nUBound)
        arrTemp(nUBound) = strNumber
        dicBaseNames(strBase) = arrTemp
    Else
        dicBaseNames.Add strBase, Array(strNumber)
    End If
Next
here are the two main functions to split the file names
Code:
Function GetSequenceNumber(strName)
    Dim oRE
    Dim colMatches
    Dim oMatch

    Set oRE = New Regexp
    oRE.Pattern = "\D*(\d*)(.*$)"
    oRE.IgnoreCase = True
    Set colMatches = oRE.Execute(strName)
    For Each oMatch In colMatches    
        GetSequenceNumber = oMatch.Submatches(0) & oMatch.Submatches(1)
        Exit Function
    Next
    GetSequenceNumber = ""
End Function

Function GetBaseName(strName)
    Dim oRE
    Dim colMatches
    Dim oMatch

    Set oRE = New Regexp
    oRE.Pattern =  "(\D*)\d*(\D*)"
    oRE.IgnoreCase = True
    Set colMatches = oRE.Execute(strName)
    For Each oMatch In colMatches    
        GetBaseName = oMatch.SubMatches(0)
        Exit Function
    Next
    GetBaseName = "ERROR"
End Function
and the output subroutine
Code:
Sub StepThruDic(objCurrentFolder, objLogFile)

    Dim strTemp
    Dim strSearch
    Dim strOutput
    Dim objNewFolder
    Dim objFile
    Dim objStream
    Dim i
            strOutput = "changelevel bug squished. Summary of changes:"
            objLogFile.writeline strOutput
For Each strBase in dicBaseNames.Keys
    For i = 1 To UBound(dicBaseNames(strBase))
    If strBase <> "c" then 'skip all c1 c2 c3 series
    strOutput = strBase & dicBaseNames(strBase)(i - 1) & ".cfg" & " " & strBase
    objLogFile.writeline strOutput
    strOutput = "  nextmap" & " " & strbase & dicBaseNames(strBase)(i)
    objLogFile.writeline strOutput
    objFSO.OpenTextFile(strBase & dicBaseNames(strBase)(i - 1) & ".cfg", 8).WriteLine _
    "nextmap" & " " & strbase & dicBaseNames(strBase)(i)
    End If
    Next
Next

End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
match string in more than one line using Regex suman9730 General .NET 0 October 24th, 2006 02:27 AM
To split values and store in the database lily611 SQL Server 2000 3 July 15th, 2004 09:01 AM
String.Split bbhill General .NET 2 April 10th, 2004 05:53 PM
store recordset in scripting.dictionary jokerdi Classic ASP Basics 0 February 17th, 2004 05:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.