UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
2 Pages V  1 2 >  (Go to first unread post)
   Reply to this topicStart new topic
> Parsing String Between 2nd And 4th Backslash "\", Access 2010    
 
   
TracyCSI
post Jan 15 2017, 11:07 PM
Post#1



Posts: 119
Joined: 9-July 10



Hello All,

I've searched this forum and can't seem to find exactly what I need.

I have the following string that I'm trying to parse.

w:\digital evidence\BRETTK\16-1459\Samsung CDMA_SM-N920V\thumbnails\

I only need the data that I listed in red. The data to the left of BRETTK (w:\digital evidence\) will always be the same number of characters, which I think is 21.

So is there a way to parse the string using the backslash character "\"? I would like the data between the 2nd and 4th "\", and I don't want the "\" to be in the result, except for the backslash between BRETTK and 16-1459.

Also, BRETTK and 16-1459 are not constant and the number of characters will vary with each record.

I'm sorry if this is confusing to anyone. I'm having a tough time trying to explain what I'm trying to accomplish. confused.gif

Thanks in advance for any help.

Tracy
Go to the top of the page
 
shadow
post Jan 16 2017, 12:09 AM
Post#2



Posts: 233
Joined: 19-April 04
From: Toronto


Tracy:

Will the data to the right of the fourth \ - i.e. Samsung CDMA_SM-N920V\thumbnails\ - remain constant?

If so, then you can calculate how many characters you are after: the length of the string, minus the first 21 characters, minus the last characters and then pick out the string you want using the mid() function.

If not, then you will have to write a little parser that will work like this:

- assign a string variable that will store your result. Let's call it strMystring
- loop through the string character by character using the mid() function until you encounter the second '/'. Of course if you are sure that it's 21 characters then you can just start parsing from character #21 smile.gif
- keep looping character by character and tack on each character to strMystring (e.g. strMystring = strMystring & mid(stringweareparsing, i, 1))
- as you loop, count slashes. Once the slash count hits 2, then break out of the loop

With this suggestion are you able to write the parsing code?

Good luck

SHADOW
Go to the top of the page
 
TracyCSI
post Jan 16 2017, 12:41 AM
Post#3



Posts: 119
Joined: 9-July 10



Hello Shadow,

No, the data to the right of the forth \ will vary.

I believe what you've described will work, but I have no clue how to write the parsing code. I've looked at examples, but haven't been able to figure it out.

Thank you!

Tracy
Go to the top of the page
 
RJD
post Jan 16 2017, 02:09 AM
Post#4


UtterAccess VIP
Posts: 7,813
Joined: 25-October 10
From: Gulf South USA


Hi Tracy: PMFJI, but here is some parsing code to fit your requirement. Also see the demo attached for a working model. There are several ways to approach this (some perhaps simpler), but I tried to use a step-by-step approach so you could see what was happening.

CODE
Public Function Part24(TheString As String) As String

Dim i As Integer
Part24 = TheString

For i = 1 To 4
Part24 = Replace(Part24, "\", "*" & i & "*", , 1)
Next

Part24 = Replace(Part24, "*3*", "\")
Part24 = Mid(Part24, InStr(Part24, "*2*") + 3)
Part24 = Left(Part24, InStr(Part24, "*4*") - 1)

End Function

HTH
Joe
Attached File(s)
Attached File  ParsingString.zip ( 20.05K )Number of downloads: 12
 

--------------------
"Each problem that I solved became a rule, which served afterwards to solve other problems."
"You just keep pushing. You just keep pushing. I made every mistake that could be made. But I just kept pushing."

Rene Descartes 1596-1650 (Mathematician and Philosopher)
Go to the top of the page
 
gemmathehusky
post Jan 16 2017, 06:35 AM
Post#5


UtterAccess VIP
Posts: 4,432
Joined: 5-June 07
From: UK


I would have thought the "right way" would be to do what you said in the question

find the position of the two separators you want, and return the string between them.

this "extract" function calls a second "findpos" function to find the position of the nth instance of a given char,
and returns the string between any two such instances. Needs some error-checking to deal with the position not being found (ie - badly formed data)


CODE
Function findpos(s As String, searchfor As String, index As Long) As Long
Dim pos As Long
Dim occur As Long
Dim startat As Long

    startat = 1
    While occur < index
        pos = InStr(startat, s, searchfor)
        If pos = 0 Then
            findpos = 0
            Exit Function
        End If
        occur = occur + 1
        If occur = index Then
            findpos = pos
            Exit Function
        End If
        startat = pos + 1
    
    Wend
    
    Exit Function

End Function

Function extract(fullstring As String, pos1 As Long, pos2 As Long, char As String) As String
    pos1 = findpos(fullstring, "\", 2)
    pos2 = findpos(fullstring, "\", 4)
    extract = Mid(fullstring, pos1 + 1, pos2 - pos1 - 1)
End Function


Sub tryit()
    MsgBox extract("w:\digital evidence\BRETTK\16-1459\Samsung CDMA_SM-N920V\thumbnails\", 2, 4, "\")
End Sub

--------------------
Dave (Male)

(Gemma was my dog)
Go to the top of the page
 
JonSmith
post Jan 16 2017, 07:30 AM
Post#6



Posts: 3,158
Joined: 19-October 10



Phew, some really complex ways of doing some simple guys. You don't need to loop, you don't need to hardcode \ positions, you don't even need to work out \ positions.


So this is actually super easy if you think of this kind of like a CSV style thing and take advantage of Split.

CODE
Dim strStartString As String
Dim strEndString As String

strStartString = "w:\digital evidence\BRETTK\16-1459\Samsung CDMA_SM-N920V\thumbnails\"
strEndString = Split(strStartString, "\")(2) & "\" & Split(strStartString, "\")(3)
Go to the top of the page
 
gemmathehusky
post Jan 16 2017, 07:49 AM
Post#7


UtterAccess VIP
Posts: 4,432
Joined: 5-June 07
From: UK


Jon.

of course! smile.gif

--------------------
Dave (Male)

(Gemma was my dog)
Go to the top of the page
 
Daniel_Stokley
post Jan 16 2017, 11:31 AM
Post#8



Posts: 246
Joined: 22-December 14
From: Grand Junction, CO, USA


Holy Cow! I don't think I've ever seen that technique for string parsing. I took your idea and had a little fun. Check this out:
CODE
Sub parseString()
    Dim strStartString As String, strEndString As String, intCounter As Integer
    strStartString = "CLM*143585226100*25***12:B:1*Y*A*Y*Y"
    For intCounter = 0 To 8
        strEndString = Split(strStartString, "*")(intCounter):    Debug.Print intCounter & ": " & strEndString
    Next intCounter
    strStartString = Split(strStartString, "*")(5)
    For intCounter = 0 To 2
        strEndString = Split(strStartString, ":")(intCounter):    Debug.Print "5 part " & intCounter & ": " & strEndString
    Next intCounter
End Sub

That produces the following:
CODE
0: CLM
1: 143585226100
2: 25
3:
4:
5: 12:B:1
6: Y
7: A
8: Y
5 part 0: 12
5 part 1: B
5 part 2: 1

Thank you Jon!
Go to the top of the page
 
MadPiet
post Jan 16 2017, 11:41 AM
Post#9



Posts: 2,251
Joined: 27-February 09



Jon,
Ha! I really like SPLIT... but I see all kinds of weird looping code here... works great until your database gets big... and then slows your database to a crawl.

Didn't realize you could refer to the subscripts returned by SPLIT directly, using SPLIT(args...)([integer])

Super handy! Thanks!
Go to the top of the page
 
RJD
post Jan 16 2017, 11:46 AM
Post#10


UtterAccess VIP
Posts: 7,813
Joined: 25-October 10
From: Gulf South USA


Jon: The Split is MUCH better than my loop. Don't know why I didn't think about that. Duh!

Regards,
Joe

--------------------
"Each problem that I solved became a rule, which served afterwards to solve other problems."
"You just keep pushing. You just keep pushing. I made every mistake that could be made. But I just kept pushing."

Rene Descartes 1596-1650 (Mathematician and Philosopher)
Go to the top of the page
 
TracyCSI
post Jan 16 2017, 01:52 PM
Post#11



Posts: 119
Joined: 9-July 10



Hello All,

You guys are the best! woohoo.gif

Thank you all for the help. I was able to get the results I need, and I couldn't be happier.

Regards,
Tracy
Go to the top of the page
 
JonSmith
post Jan 16 2017, 04:18 PM
Post#12



Posts: 3,158
Joined: 19-October 10



Hahaha, happy to help everyone and glad members old and new got something out of the solution.

Split is fantastic in many circumstances.
I used it to really simplify getting say the folder from a full file path, or remove x folders from the end of a path to get the parents.

If you using split to break apart along the \ and then redim your array with one dimension less (or x dimensions less) it will chop off the end of the path. You can then use Join to put the array back together into a string with the \ separators.
Its great to do it without a loop again and works very consistently / flexibly.
Go to the top of the page
 
AlbertKallal
post Jan 17 2017, 12:00 PM
Post#13


UtterAccess VIP
Posts: 2,551
Joined: 12-April 07
From: Edmonton, Alberta Canada


And if one is looping over the results, then I often use this:

CODE
    Dim strStartString     As String
    Dim s                  As Variant
    
    strStartString = "CLM*143585226100*25***12:B:1*Y*A*Y*Y"
    
    For Each s In Split(strStartString, "*")
       Debug.Print s
    Next


So don't forget for/each - it often means you don't have to determine the number of loops you require.

Regards,
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
kallal@msn.com
Go to the top of the page
 
TracyCSI
post Jan 17 2017, 12:54 PM
Post#14



Posts: 119
Joined: 9-July 10



Hello again,

I have one more question, please.

I'm using Joe's code
CODE
Public Function Part24(TheString As String) As String

Dim i As Integer
Part24 = TheString

For i = 1 To 4
Part24 = Replace(Part24, "\", "*" & i & "*", , 1)
Next

Part24 = Replace(Part24, "*3*", "\")
Part24 = Mid(Part24, InStr(Part24, "*2*") + 3)
Part24 = Left(Part24, InStr(Part24, "*4*") - 1)

End Function


And it appears I need some error-checking. I found the majority of the strings will look like this, "w:\digital evidence\BRETTK\16-1459\Samsung CDMA_SM-N920V\thumbnails\", however, there are some that won't have characters after BRETTK\, like this: "w:\digital evidence\BRETTK\". By the way, BRETTK is variable and could be any name.

If someone could please plug in some error-checking so if the string stops after BRETTK\, it won't return an error. I really don't know how to add error-checking.

Thank you so much.
Tracy

Th
Go to the top of the page
 
Daniel_Stokley
post Jan 17 2017, 12:54 PM
Post#15



Posts: 246
Joined: 22-December 14
From: Grand Junction, CO, USA


Thank you Jon and Albert. It just gets better.
Albert's For Each loop is definitely better than my For loop with a counter. Much more versatile.
Go to the top of the page
 
Daniel_Stokley
post Jan 17 2017, 01:08 PM
Post#16



Posts: 246
Joined: 22-December 14
From: Grand Junction, CO, USA


Hi Tracy,

I've taken Albert's code and come up with the following:
CODE
    Debug.Print "Tracy's problem"
    strStartString = "w:\digital evidence\BRETTK\"
    intCounter = 0
    For Each s In Split(strStartString, "\")
       intCounter = intCounter + 1
       Debug.Print intCounter & ": " & s
       If (intCounter = 4) And (s = "") Then
          Debug.Print "Nothing after the user name."
       End If
    Next

That bit of code produces the following:
CODE
Tracy's problem
1: w:
2: digital evidence
3: BRETTK
4:
Nothing after the user name.

Maybe that will give you some help towards the necessary error checking. The idea is that if the 4th part, the part after the user name, is blank, then you need to do something special.

As the DBGuy would say, just my 2 cents smile.gif
Go to the top of the page
 
JonSmith
post Jan 18 2017, 04:08 AM
Post#17



Posts: 3,158
Joined: 19-October 10



Hi Tracey,

You can always try the code I presented. Its much simpler then the loop and shouldn't need the error checking.


JS
Go to the top of the page
 
TracyCSI
post Jan 18 2017, 09:10 PM
Post#18



Posts: 119
Joined: 9-July 10



First off, my apologies to Jon. It certainly looks like your code is much simpler and will get the job done, but I just can't get it figured out.

For the most part, I was able to get the results I need using the posted Code, however, I’m still having a problem parsing my string. All works well if the string looks like this:
w:\digital evidence\BRETTK\16-001459\Samsung CDMA_SM-N920V\files\Image\

(The string configuration is: W:\ is the server drive. “Digital Evidence” is a folder and it will be constant. “BRETTK” is a folder and the name will change. “16-001459” is a folder and the name will change. Everything after the “16-001459” folder gets parsed as I don’t need that data.)

However, there are some strings that do not have a folder after third \, like below,
w:\digital evidence\BRETTK\

When this happens, I receive a Run-time error ‘5’, Invalid procedure call or argument.
And the below line of code is highlighted:
Part24 = Left(Part24, InStr(Part24, "*4*") - 1)

I’ve posted the code I'm using below. Can someone please add to the code so the errors will stop occurring? I’m really weak in this area of coding and any help would be appreciated.

Regards,
Tracy

CODE
Public Function Part24(FPath As String) As String

Dim i As Integer
Part24 = FPath

For i = 1 To 4
Part24 = Replace(Part24, "\", "*" & i & "*", , 1)
Next

Part24 = Replace(Part24, "*3*", "\")
Part24 = Mid(Part24, InStr(Part24, "*2*") + 3)
Part24 = Left(Part24, InStr(Part24, "*4*") - 1)


End Function
Go to the top of the page
 
RJD
post Jan 18 2017, 11:25 PM
Post#19


UtterAccess VIP
Posts: 7,813
Joined: 25-October 10
From: Gulf South USA


Hi: Here's a quick-and-dirty solution to your "only three \ characters" issue, using a mod to my previous code (which you are using above). No doubt Jon will want to adjust his code to accommodate this new information, and I'll let him do that. But you can use this until you get that.

This assumes there will always be at least 3 back slashes.

CODE
Public Function Part24(TheString As String) As String

Dim i As Integer, s As Integer
Part24 = TheString

For i = 1 To 4
Part24 = Replace(Part24, "\", "*" & i & "*", , 1)
Next

Part24 = Replace(Part24, "*3*", "\")
Part24 = Mid(Part24, InStr(Part24, "*2*") + 3)
If Len(TheString) - Len(Replace(TheString, "\", "")) > 3 Then
Part24 = Left(Part24, InStr(Part24, "*4*") - 1)
End If

End Function

(tested)

HTH
Joe

--------------------
"Each problem that I solved became a rule, which served afterwards to solve other problems."
"You just keep pushing. You just keep pushing. I made every mistake that could be made. But I just kept pushing."

Rene Descartes 1596-1650 (Mathematician and Philosopher)
Go to the top of the page
 
JonSmith
post Jan 19 2017, 05:47 AM
Post#20



Posts: 3,158
Joined: 19-October 10



Ok, I fully fleshed out into a real function and added comments so you can see how it works.
This will work for any kind of string as long as at least one of the delimiters is in the string. If you try to get part of the string that isn't there it will just be ignored.

I have designed it to be very flexible so you can specify the delimiter so it doesn't have to be \ and used a ParaArray for the string positions which means you aren't limited to only 2 chunks as you were before. You may not be familiar with ParamArray but it basically allows you to enter a parameter again and again at the end of the parameters. They are then assigned to an array that you can use.

For examples you can use
GetStringParts("w:\digital evidence\BRETTK\16-1459\Samsung CDMA_SM-N920V\thumbnails\", "\", 0) 'to just get the drive path

GetStringParts("w:\digital evidence\BRETTK\16-1459\Samsung CDMA_SM-N920V\thumbnails\", "\", 0, 1) 'gets the drive path and first folder

GetStringParts("w:\digital evidence\BRETTK\16-1459\Samsung CDMA_SM-N920V\thumbnails\", "\", 2, 3, 4, 5)' gets "BRETTK\16-1459\Samsung CDMA_SM-N920V\thumbnails"



CODE
Function GetStringParts(strStartString As String, strDelimiter As String, ParamArray arrStringPostitions()) As String
'---------------------------------------------------------------------------------------
'    Method: GetStringParts
'    Author: JonSmith
'      Date: 19/01/2017
'   Purpose: Takes a string and splits it appart. Returns the parts of the string specified.
'            Remember that arrays are zero based so the first position is 0 and not 1
'   Returns: Parts of the split string
'Parameters: strStartString - the string we want to split.
'            strDelimiter - The delimiter used for the split
'            arrStringPostitions - ParamArray of all the string parts to be retrieved.
'     Usage: GetStringParts("w:\digital evidence\BRETTK\16-1459\Samsung CDMA_SM-N920V\thumbnails\", "\", 3,4)
'---------------------------------------------------------------------------------------
Dim i As Integer 'For looping
Dim arrSplitString() As String 'An array to store the split string

    'Split the string out to the different parts so we can grab what we need.
    arrSplitString = Split(strStartString, strDelimiter)
    
    'Loop through the paramarray, check to see if that position exists in the split string and if so get it.
    For i = 0 To UBound(arrStringPostitions)
        If arrStringPostitions(i) <= UBound(arrSplitString) Then _
            GetStringParts = GetStringParts & arrSplitString(arrStringPostitions(i)) & strDelimiter
    Next
    
    'Remove the trailing delimiter
    If GetStringParts <> vbNullString Then _
        GetStringParts = Left(GetStringParts, InStrRev(GetStringParts, strDelimiter) - 1)
End Function
Go to the top of the page
 
2 Pages V  1 2 >


Custom Search
RSSSearch   Top   Lo-Fi    10th December 2017 - 09:25 PM