Nikita Maximov
Nikita Maximov

Reputation: 21

Reg Exp Capture everything before a certain word in VBA

Hi I want to capture everything before the word contact Eg:

15 Lecky Road
Ballinderyy
Upper Lisburn
BT28 2QA
Contact: Anna Murphy
Telephone: 02892 610634
Fax: 02892 610635

This is my regexp:

(.|\n)*Contact$

Thanks

Upvotes: 0

Views: 149

Answers (2)

Pierre
Pierre

Reputation: 1046

I'd use:

answer=split(s,"Contact:")(0)

Far faster than regexp

Upvotes: 1

SamWhan
SamWhan

Reputation: 8332

If you really want to do it with regex, this should do it for you:

^([\s\S]+)\s*Contact

It captures everything, including line feeds and stuff, up to the last occurrence of the word Contact. If there are multiple Contact's and you just want to capture up to the first, change the RE to be non-greedy:

    ^([\s\S]+?)\s*Contact

See it here at regex101.

Upvotes: 0

Related Questions