Avatar of peispud
peispud
Flag for Canada asked on

Need a regex pattern to find a string between two strings

Hi
I am looking to get the customer's name  from a multi-page computer generated document.  In these documents, then name of the customer always follows the pattern shown below.  


1) Customer Information                  ----- (string constant)
2) Customer                                       ------ (string constant)
3) Customer Name                           ------ (information being sought)
4) Contact Telephone Number ----------  (string constant)

It actually looks like this in the document.  There is a line feed after each string constant  and customer name.

Customer Information
Customer
John Doe
Contact telephone number


There would only be one instance of a customer per document. Could someone help me with the regex pattern please.  

Thank you.
Regular Expressions

Avatar of undefined
Last Comment
peispud

8/22/2022 - Mon
Rgonzo1971

Hi,

pls try EDITED
Customer\n(.*)\nContact Telephone Number

Open in new window

REgards
peispud

ASKER
Nope..  it didn't work for me.

Just in case, I will include the code.  Perhaps I've got something else wrong.

 Public Function ExtractTMIData(ByVal strXX As String) As Ticket()
    Dim reg As New regExp
'    reg.Pattern = "Customer Information\s+Customer"
    reg.Pattern = "Customer\n(.*)\nContact Telephone Number"
    reg.Global = True
    reg.IgnoreCase = True
    Dim Matches As MatchCollection
    Set Matches = reg.Execute(strXX)
End Function

Open in new window

Rgonzo1971

Maybe replace \n with \n\r
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Rgonzo1971

then try
 Public Function ExtractTMIData(ByVal strXX As String) As Ticket()
    Dim reg As New regExp
'    reg.Pattern = "Customer Information\s+Customer"
    reg.Pattern = "Customer\n\r(.*)\n\rContact Telephone Number"
    reg.Global = True
    reg.IgnoreCase = True
    Dim Matches As MatchCollection
    Set Matches = reg.Execute(strXX)
ExtractTMIData = Matches(0).SubMatches(0)
End Function

Open in new window

Rgonzo1971

Could you send a dummy file?
peispud

ASKER
Nope.  

The last command
ExtractTMIData = Matches(0).SubMatches(0)

Open in new window

generated an error code.  
Even with this commented out,  I did not get the customer name.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
peispud

ASKER
Ok..  here is a file with the important information changed.

I need to extract "John Doe"
peispud

ASKER
Oops... Here is the file
Test.docx
SOLUTION
Rgonzo1971

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.
ASKER CERTIFIED SOLUTION
Dan Craciun

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dan Craciun

Dim myMatch As Match
Dim myMatches As MatchCollection
Dim myRegExp As RegExp
Set myRegExp = New RegExp
myRegExp.Pattern = "Customer\r?\n(.*)"
Set myMatches = myRegExp.Execute(SubjectString)
For Each myMatch In myMatches
	'matched text: myMatch.Value
	'match start: myMatch.FirstIndex
	'match length: myMatch.Length
	'backreference n text: myMatch.SubMatches(n-1)
Next

Open in new window


This is for VB6.
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
peispud

ASKER
Thank you both for your help.

This is awesome!!