Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract Characters from Left through pattern "||"

Status
Not open for further replies.

jimdltn

Technical User
Dec 10, 2004
7
I have a key text field (created in Cache)i.e., 1||987, 2345||2, 567||24. What I want to extract are the number (characters) after the || pattern. So I want to return 987, 2, 24. A little help would be appreciated.
 
You could Split at the comma and then use InStr on each element of the array.
 
Regular expressions would handle this quite well also.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 

Use Split() at the comma and then Split() at the || and grab the 2nd element
 
Ahahaha! Regular Expressions [smile]

Seriously though, this UDF will work (paste it into a module):
Code:
Function FindStringNumbers(strInput As String) As String
Dim objReg As Object
Dim objMatches As Object
Dim objMatch As Object

Set objReg = CreateObject("VBScript.RegExp")

With objReg
    .IgnoreCase = True
    .Multiline = False
    .Global = True
    .Pattern = "\|\|[0-9]*"
    Set objMatches = .Execute(strInput)
    
        For Each objMatch In objMatches
            FindStringNumbers = FindStringNumbers & Replace(Trim(objMatch), "||", "") & " "
        Next objMatch
End With

FindStringNumbers = Replace(Trim(FindStringNumbers), " ", ",")

Set objReg = Nothing

End Function
Call it like this:
Code:
Results: FindStringNumbers([YourFieldName]
Please bear in mind I've knocked this up very quickly so some of the replacing etc. could be done better but it is working on your test data.

If you would like an explanation of how and why it works I'll be more than happy to oblige.

Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Oops, thought I was in the Jet SQL forum, the function will still work fine, just don't call it like a field in a query [blush]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top