1

Sample text

5950 S Willow Dr Ste 304 
 Greenwood Village, CO 80111
 P (123) 456-7890
 F (123) 456-7890
 Get Directions 

Tried the following but it grabbed the first line of the address as well

(.*)(?=(\n.*){2}$)

Also tried

P\s(\(\d{3})\)\s\d+-\d+

but it doesn't work in WebHarvy even though it works on RegexStorm

Looking for an expression to match the phone and fax numbers from it. I would be using the expression in WebHarvy

https://www.webharvy.com/articles/regex.html

Thanks

pb_ng
  • 361
  • 1
  • 5
  • 19

1 Answers1

2

Your second pattern is almost what you need to do. With P\s(\(\d{3})\)\s\d+-\d+, you captured into Group 1 only (\(\d{3}) part, while you need to capture the whole number.

I also suggest to restrict the context: either match P as a whole word, or as the first word on a line:

\bP\s*(\(\d{3}\)\s*\d+-\d+)

or

(?m)^\s*P\s*(\(\d{3}\)\s*\d+-\d+)

See the regex demo, and here is what you need to pay attention to there:

enter image description here

The \b part matches a word boundary (\b) and (?m)^\s* matches the start of a line ((?m) makes ^ match the start of a line) and then \s* matches 0+ whitespaces. You may change it to only match horizontal whitespaces by replacing the pattern with [\p{Zs}\t]*.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563