0

I'm trying to select with regex the (fake) phone number in the code below. Sometimes, the phone number has spaces and sometimes it doesn't. Obviously, I want to avoid selecting the other numbers, and I would also like to avoid the + symbol.

Any help would be greatly appreciated.

Example 1:

Test Meeting 
Mon, Jan 8, 2018 5:00 PM - 5:30 PM AEDT 

Please join my meeting from your computer, tablet or smartphone. 
https://example.com/join/132124483

You can also dial in using your phone. 
Australia: +61 2 3017 3203

Access Code: 132-124-483 

First meeting? Let's do a quick system check: https://link.example.com/system-check 

Example 2:

Conference link and details to come. 
──────────────────────────────────────────
Please join my meeting from your computer, tablet or smartphone.
https://example.com/join/829203911

You can also dial in using your phone.
Singapore (Toll Free): 18007932321

Access Code: 829-203-911

First GoToMeeting? Try a test session: https://care.example.com/g2m/getready
──────────────────────────────────────────
Matt Hough
  • 1,049
  • 2
  • 13
  • 27
  • I think this would help you : https://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation for online regex testing I would suggest this: https://regex101.com/ – xSkrappy Jan 09 '18 at 00:27
  • Is the substring `: +` always before your number? Does it end with a end of line? More precisions about the phone number format? Use the context as much as possible. – Casimir et Hippolyte Jan 09 '18 at 00:33
  • @xSkrappy Unfortunately, the other stack overflow link wasn't much help considering all the other numbers surrounding. @Casimir et Hippolyte In another example, the number doesn't come with the + beforehand. I don't really want to select the `+` anyway, but if they all came with the `+` it would be as simple as selecting the numbers with the plus and stripping it. :( I have just added another example with a different number – Matt Hough Jan 09 '18 at 00:38
  • Is there always a country name at the start of the line and after eventual other characters a colon and a space before the number (with or without a `+`)? One more time, does the number ends the line? – Casimir et Hippolyte Jan 09 '18 at 00:42
  • Yes and yes. I'm unsure of how the country is typed beforehand though. Sometimes it might have `(Toll Free)` beforehand, sometimes not. – Matt Hough Jan 09 '18 at 00:49

1 Answers1

1

Without all informations, I suggest this pattern:

: \+?\K\d+(?: \d+)*$

Perhaps it is better to add an exhaustive list of countries in an alternation anchored to the start of the line before. It depends of your requirements. Something like this:

^(?:Argentine|France|Singapoure)\b[^\n\r:]*: \+?\K\d+(?: \d+)*$

(all on the left of \K isn't in the whole match result).

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • 1
    Be careful, in this case try to add eventual horizontal spaces `\h*` before `$` to be sure that the line isn't something else. Also test the pattern on http://rubular.com (that is designed for ruby), not with regex101 or other. – Casimir et Hippolyte Jan 09 '18 at 01:01