Single out everything after last occurence of a 10 number string

Viewed 36

I'm trying to get everything from the start to the end of the last phone number (In short all contact information email and phone numbers).

'Firstname Lastname lorem@gmail.com 0000243598 CAB pilote 001 001',
'Firstname Lastname lorem@gmail.com 0100753503 CAB pilote 001 001',
'Firstname Lastname lorem@gmail.com 0033555381 0022243578 CAB pilote 001 001',
'Firstname Lastname lorem@gmail.com 0400253598 0000664248 CAB pilote 001 001',

So far, I already have a custom solution that is 10 foot long but I'm searching for something elegant along the lines of \d{10,}(.*)$ tho, when a user has 2 phone numbers it fails to retrieve both.

I was hoping to single out "everything after the last occurence of a, at least, 10 numbers string, but failed to do so. Any help is welcome.

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

The regex has to be against the phone number has from time to time the stuff that comes after the last phone number change in length and content.

2 Answers

You may use this regex in PCRE:

.*\h\K\d{10,}[^']*

RegEx Demo

RegEx Details:

  • .*: Match 0 or more of any characters (greedy match)
  • \h: Match a whitespace
  • \K: Reset the match
  • \d{10,}: Match 10 or more digits
  • [^']*: Match 0 or more of non-' characters

PS: If PCRE is not supported then use a capture group:

.*\s(\d{10,}[^']*)

If I am reading it right, and you want to

I'm trying to get everything from the start to the end of the last phone number

Then you can match the ' at the start of the string, and capture until the last occurrence of 10 or more digits in group 1.

^'(.* \d{10,})

Regex demo

const regex = /^'(.* \d{10,})/gm;
const str = `'Firstname Lastname lorem@gmail.com 0000243598 CAB pilote 001 001',
'Firstname Lastname lorem@gmail.com 0000243598 CAB pilote 001 001',
'Firstname Lastname lorem@gmail.com 0000243598 0000243598 CAB pilote 001 001',
'Firstname Lastname lorem@gmail.com 0000243598 0000243598 CAB pilote 001 001',`;

console.log(Array.from(str.matchAll(regex), m => m[1]));

If the quotes are not part of the string, then you can omit the group:

^.* \d{10,}
Related