We help IT Professionals succeed at work.

find text pattern in a string

PeterBaileyUk
on
372 Views
Last Modified: 2012-05-10
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

Comment
Watch Question

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

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

1.6i

is Number Period Number i

??

mx

Author

Commented:
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! ;)

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
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  

?

mx
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
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));

mx
CERTIFIED EXPERT
Top Expert 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2011

Commented:
For what it's worth

Replacing:
"\b[0-9].[0-9]i\b"
in any of the above with
\b[0-9]+.[0-9]+i\b

will support for example 22.33.i

Chris
CERTIFIED EXPERT
Top Expert 2011

Commented:

will support for example 22.33i

for example ... doh!

Author

Commented:
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.

CERTIFIED EXPERT
Top Expert 2011

Commented:
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.

Chris
CERTIFIED EXPERT
Top Expert 2011

Commented:
"\b[0-9]+.[0-9]+[A-Z]{0,2}I\b"

Will allow 1.2i as well as 2.3eSi

Chris

Author

Commented:
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.

Author

Commented:
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

CERTIFIED EXPERT
Top Expert 2011

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

Chris
CERTIFIED EXPERT
Top Expert 2011

Commented:
Sorry, RTFQ!, which I have now done!

Chris
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

CERTIFIED EXPERT
Top Expert 2011

Commented:
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")

Chris
CERTIFIED EXPERT
Top Expert 2011

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

i.e.
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.

Chris
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
                Next
            Else
                ReDim retArray(0)
                retArray(0) = colmatch(0)
            End If
        End With
        getDatabyRegEx = retArray
    Else
        getDatabyRegEx = Array("")
    End If
    
End Function

Open in new window

Author

Commented:
i will be using vba for this but was just testing

Author

Commented:
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)
CERTIFIED EXPERT
Top Expert 2011

Commented:
You have both subs getDatabyRegEx  & valDatabyRegEx in module 1?

Chris

Author

Commented:
i have regexpfind and regexpreplace in module one.

Author

Commented:
i missed it now its installed

Author

Commented:
excellent thank you
CERTIFIED EXPERT
Top Expert 2011

Commented:
Glad it helped

Chris
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

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

mx

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.