Home > Back-end >  Need help finding a regex pattern for extracting a data from a data string
Need help finding a regex pattern for extracting a data from a data string

Time:09-05

I am trying to extract the phone number and full name from a data string, but I can't figure out a regex pattern needed for extracting the said data. Can anyone help me with it?

This is the data string:

Sat, September 3rd, 10:13am - Case Lead ID: #C69-660-301C

John Doe | [email protected] | 5551234567

FYI, I found a regex pattern to extract email and it worked.

([a-zA-Z0-9._-] @[a-zA-Z0-9._-] \.[a-zA-Z0-9_-] )

BTW, the tool I use to extract this data only supports positive lookahead regex patterns.

CodePudding user response:

If your data is always structured like this on a single line with 2 pipes, you can use a negated character class with 3 capture groups.

Assuming there are no pipes in the data, and the delimiter is fixed like |

^([^|\n] ) \| ([^|\n] ) \| ([^|\n] )$

Regex demo

Or a bit more specific matching an email like pattern and 10 digits in the last part:

^([^|\n] ) \| ([^\s@] @[^\s@] ) \| (\d )$

Regex demo

CodePudding user response:

For a phone number:

\d{10} # will match 10 digits
\d{3}-\d{3}-\d{4} # will match a ten digit phone number with dashes

For a full name, assuming the first letters of the first and last name are capitalized and that there is a single space between the names, which is not always the case:

[A-Z][a-z] \ [A-Z][a-z] 

If there are other number formats that you may need, or names that have punctuation, accent marks, or initials, I can amend my answer.

In case it is helpful, this is the website that I use for practicing Regex: https://regexr.com/

  • Related