Avatar of peispud
peispud
Flag for Canada asked on

Use Regex to extract string between two words in a multi line string.

HI.

I am using Microsoft Access VBA in a Windows 10 machine.
I am trying to extract data from a multi-line string copied from a web page whose format is always the same.

By example , one of the lines in the web page would be

Circuit Name:           <data item 1>            Issued by:
Customer Name      <data item 2>            Attn

I would like the regex pattern that would give me the  <data item 1>   ..   Also,  <data Item 2>

Thank you.
Regular ExpressionsMicrosoft Access

Avatar of undefined
Last Comment
Zakaria Acharki

8/22/2022 - Mon
Zakaria Acharki

Hi peispud,

It could be something like :

Set objRegExp_1 = CreateObject("vbscript.regexp")
objRegExp_1.Global =true
objRegExp_1.MultiLine =true
objRegExp_1.IgnoreCase=true
objRegExp_1.Pattern =(?<=\<)(.*?)(?=\>)

Set regExp_Matches = objRegExp_1.Execute("your multiline string here")

Open in new window

(Not tested yet)
Zakaria Acharki

You could loop through the matches like :

If regExp_Matches.Count <> 0 Then
        For Each match In regExp_Matches
                Debug.Print match.Value
        Next
End If

Open in new window


Here's a live example of the pattern :

https://regex101.com/r/FOcxzF/1

To return the greater/lower-than signs you could use :

https://regex101.com/r/UMDKmh/1
peispud

ASKER
Thank you...

But I am looking for the text between "Circuit Name:"  and "Issued By" (with no n-print characters removed).

The pattern does make reference to this.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Zakaria Acharki

You're welcome,

Not sure what you meant by n-print characters.
peispud

ASKER
The function would look something like this

Private Function Regex_GetInfo(ByVal strMultiline As String) As String
    Dim str1 As String: str1 = "Circuit Name:"
    Dim str2 As String: str2 = "Issued by:"

    Set objRegExp_1 = CreateObject("vbscript.regexp")
    objRegExp_1.Global = True
    objRegExp_1.Multiline = True
    objRegExp_1.IgnoreCase = True
    objRegExp_1.Pattern = "<(?<=\<)(.*?)(?=\>)"
   
    Set regExp_Matches = objRegExp_1.Execute(strMultiline)
   
   
    If regExp_Matches.Count = 1 Then
        'MsgBox ("This string is a valid email address.")
        Regex_GetInfo = Match.Value
    End If
End Function
ASKER CERTIFIED SOLUTION
Zakaria Acharki

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
peispud

ASKER
You are correct!!!

Thank you very much. Happy holidays!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Zakaria Acharki

Welcome anytime, glad to help brother.

Happy New Year!