Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk

asked on

find text pattern in a string

I have a string:

 106 1.6i 16V

I would like a function that can identify if the string contains 1.6i so: number period number followed by the i.

it can return true or false

of course the numbers will change ie 1.8, 1.4 etc

regards in advance

Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

" number period number followed by the i."
Well, that's not what you are showing


is Number Period Number i


Avatar of PeterBaileyUk


looks the same to me but yes: Number Period Number i, tell me what you interpreted so that I wont make the same mistake again! ;)

wow lol ... it IS the same. Man, I kept looking at that and seeing something else. It MUST be late ...

So ... would there ever be more than one Period or more than one i ?  Because if not, that seems all you would have to do is detect the period or the i  ... ?  Especially if the format is always
number period number i  


Well, I need to zzzz ... but in case what I asked is the case ... then you can detect the i or .  like so, using the InStr() function

If InStr(1, YourString ,".") > 0 Then
    ' Do what evern
End If

Or in a query like so:
SELECT Table1.*
FROM Table1
WHERE (((InStr(1,[FIELD1],".")>0)=True));

Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For what it's worth

in any of the above with

will support for example 22.33.i


will support for example 22.33i

for example ... doh!
what could change is 1.3si etc the nominal cc ie 1.2, 2.3 etc will change the i tagged on the end represents fule delivery method. I have a scoring system that does various tests but in one example two vehicles with the same engine get the same score the only difference was the fuel delivery. An expert gave me advice before on a function to get the nom cc from a string ie the 1.2 etc and I suspect that was chris as the form is similar to your suggestion.

To try and capture the variations ... we do need to know the variations.  for example we could allow any alpha before the i or only specific ones.

The more info you can provide (on permutations) the more we can do to help with an appropriate solution.


Will allow 1.2i as well as 2.3eSi

I gouped the textual descriptions but there were too many so if I start off little and see where it leads with the test itself, rather than try and show every permutation. just off to experiment.
from the immediate window i did:
?getDatabyRegEx("106 1.6i 16V","\b[0-9]+.[0-9]+[A-Z]{0,2}I\b")(0)

but it says:
valDatabyRegEx not defined

Did you add the supplied sub to a normal code module?

Sorry, RTFQ!, which I have now done!

Function valDatabyRegEx(strFindin As String, strPattern As String, Optional bolMatchCase As Boolean = False) As Boolean
    With CreateObject("vbscript.regexp")
        .IgnoreCase = Not bolMatchCase
        .Pattern = strPattern
        valDatabyRegEx = .test(strFindin) = True
    End With
End Function

Open in new window

Reading the original question ... you actually wanted to know if a matching string is there whereas the suggested code returns THE value found.

Therefore to simply return true/false reflecting found then use:

Noting a bug ... heaven forbid use:

?getDatabyRegEx("106 1.6]3i 16V","\b[0-9]+\.[0-9]+[A-Z]{0,2}I\b")

Use of getDatabyRegEx if the string is not found returns an error via the command line so that's fixed as below ... hopefully

msgbox getDatabyRegEx(" 106 1.6;i 16V", "\b[0-9]\.[0-9]i\b")(0)
was returning an error but with the below modification doesn't any longer.

Function getDatabyRegEx(strFindin As String, strPattern As String, Optional strReplacement As String = "", Optional bolGlobalReplace As Boolean = True, Optional bolMatchCase As Boolean = False) As Variant
Dim colmatch As Object
Dim itm As Variant
Dim retArray() As String
Dim intBounds As Integer

    intBounds = -1
    If valDatabyRegEx(strFindin, strPattern, bolMatchCase) Then
        With CreateObject("vbscript.regexp")
            .IgnoreCase = Not bolMatchCase
            .Global = bolGlobalReplace
            .Pattern = strPattern
            Set colmatch = .Execute(strFindin)
            If bolGlobalReplace Then
                For Each itm In colmatch
                    intBounds = intBounds + 1
                    ReDim Preserve retArray(0 To intBounds)
                    retArray(intBounds) = itm
                ReDim retArray(0)
                retArray(0) = colmatch(0)
            End If
        End With
        getDatabyRegEx = retArray
        getDatabyRegEx = Array("")
    End If
End Function

Open in new window

i will be using vba for this but was just testing
interesting i still get sub or function not defined, the code is in module 1 and calling :
?getDatabyRegEx("106 1.6i 16V","\b[0-9]+.[0-9]+[A-Z]{0,2}I\b")(0)
You have both subs getDatabyRegEx  & valDatabyRegEx in module 1?

i have regexpfind and regexpreplace in module one.
i missed it now its installed
excellent thank you
Glad it helped

I see this Q escalated a bit and there was more than meets the eye  :-)