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!

Finding the position of a LIKE match in a string??? 1

Status
Not open for further replies.

knifey

Technical User
Nov 14, 2006
180
GB
Hi,
Is it possible to get the position of a pattern match in a string that has been found using the LIKE operator?
This is what I have so far.....
Function GetDateFromString(strVar As String) As String
Dim strVarLen As Long
strVarLen = Len(strVar)
If strVar Like "*##.##.##*" Then
end if
End Function
All I need now is a way to tell the position of the pattern match so I can extract using MID, LEN. But this is proving more difficult than I'd imagined.
Any advice or pointers would be much appreciated.
Thanks,
K
 


Hi,

Do you have several typical examples of such strings?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
You could try:
Code:
Function GetDateFromString(strVar As String) As String
Dim i As Long
If strVar Like "*##.##.##*" Then
For i = 1 To Len(strVar) - 8
  If Mid(strVar, i, 8) Like "##.##.##" Then
    Exit For
  End If
Next
  GetDateFromString = i
Else
GetDateFromString = 0
End If
End Function

Cheers
Paul Edstein
[MS MVP - Word]
 


And why would you want to return a STRING for a DATE, if that's what the purpose is?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Of course if you want the actual string, you could use something like:
Code:
Function GetDateFromString(strVar As String) As String
Dim i As Long
If strVar Like "*##.##.##*" Then
For i = 1 To Len(strVar) - 8
  If Mid(strVar, i, 8) Like "##.##.##" Then
    Exit For
  End If
Next
  GetDateFromString = Mid(strVar, i, 8)
Else
  GetDateFromString = "N/A"
End If
End Function

Cheers
Paul Edstein
[MS MVP - Word]
 

Modifying Paul's code accordingly to return a DATE if indeed there is a string that represents a date...
Code:
Function GetDateFromString(strVar As String) As Variant
    Dim i As Long, a
    If strVar Like "*##.##.##*" Then
        For i = 1 To Len(strVar) - 8
          If Mid(strVar, i, 8) Like "##.##.##" Then  '[b]
            a = Split(Mid(strVar, i, 8), ".")[/b]
            Exit For
          End If
        Next
'[b][highlight]assuming that ##.##.## is yy.mm.dd structure[/highlight]
          GetDateFromString = DateSerial("20" & a(0), a(1), a(2))
[/b]        Else
        GetDateFromString = 0
    End If
End Function

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,

I wasn't about to assume anything about the data order in the string, or the century to which it belongs.

Cheers
Paul Edstein
[MS MVP - Word]
 
How about:
Code:
Public Function GetDate(strvar As String) As Variant
Dim pos As Long
GetDate = "No Date found"
pos = 0
If strvar Like "*##.##.##*" Then
    Do
        pos = InStr(pos + 1, strvar, ".")
        GetDate = Mid(strvar, pos - 2, 8)
    Loop While Not GetDate Like "##.##.##"
End If
End Function
Tony
 
Hmm, something weird going on here. Just before I posted my reply, there were no responses, but I seem to have come in after a whole mailbag from Skip & Macropod. Sorry chaps.

Bearing in mind Skip's comment about dates, you could do the same to the code I posted.

Note, this only works if your date really is in the format ##.##.##. So it would miss 7.9.02, or 10/11/92.

Tony
 


Just an axample. I hate 2-digit year shortcuts. You have to test the value of yy if .LT. some value then assume twentyfirst century otherwise twentieth.

But of course, it is even possible that the ##.##.## could represent a date that is neither in this century or the last, in which case a date in Excel, MIGHT be unrepresentable.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
'Like' uses a bastardised version of regular expressions - so why not use the full regexp functionality.

For example:
Code:
[blue]Public Function GetMatchPosFromString(strVar As String) As String
    Dim myitem As Object
    
    GetMatchPosFromString = -1 ' so we get similar results as Instr
    With New RegExp
        .Pattern = "\d\d\.\d\d\.\d\d"
        For Each myitem In .Execute(strVar)
            GetMatchPosFromString = myitem.FirstIndex
        Next
    End With
    
End Function[/blue]
 


non illegitimi carborundum ;-)

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Don't let the b*st*rds grind you down. Not heard, or even thought of, that one for years!!!!!!!

Many thanks,
D€$
 


it was a rather obtuse reference to strongm's post.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi,
All worked great but I've gone fopr macropod's version.
Thanks again,
K
 



What do you intend to do with the position value?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top