Link to home
Start Free TrialLog in
Avatar of peispud
peispudFlag for Canada

asked on

Regex Find word within string

Hi.  I am having some trouble to get the pattern for the following

Match criteria
1)   word begins with 4 alphabetical characters
2)  immediately followed by "PE"
3)  get the rest of the word

string to be searched  =  "This is the sample string abcdPEy2c4s  where I hope to get that weird word."


I would like the result to be abcdPEy2c4s
ps.. The string may be multiline.

Thank you.
SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peispud

ASKER

Thanks Saige.

I tried it, but came up with  error  "method 'multi line' of object 'regexp 2' failed"
Here is the complete code.  Any ideas?

dim str as str : str = "smsdpe01k01"
    Dim reg As New regExp
    reg.pattern = "(?<=PE).*$"
    reg.Global = True
    reg.IgnoreCase = True

    Dim Matches As MatchCollection
    Set Matches = reg.Execute(str)

    Dim theanswer As String
    theanswer = Matches.Item(0)

Open in new window

Avatar of peispud

ASKER

Also,  I should mention that I am using VBA in Microsoft Access.
Avatar of peispud

ASKER

Correction
The code segment that I sent earlier was in error.  

    Dim str As String
    str = "smsdpe01k01"
    Dim reg As New regExp
    reg.pattern = "(?<=[a-zA-Z]{4}PE).*$"
    reg.Global = True
    reg.IgnoreCase = True

    Dim Matches As MatchCollection
    Set Matches = reg.Execute(str)

    Dim theanswer As String
    theanswer = Matches.Item(0)

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peispud

ASKER

Thank you all.
Bill Prew and Subodh Tiwari's reply were perfect.   I also appreciate IT_Saige's answer.
You're welcome Peispud! Glad it worked as desired.