Jesse Don
Jesse Don

Reputation: 13

Extract name from name card details

Can anyone tell me how to extract the name from the below detail?

Singapore Polytechnic 

Daniel Rossey
Manager

66556652
[email protected]

I just want to match only name "Daniel Rossey" and my current expression below does not work

^[A-z][A-z|\.|\s]+$

It will match "Manager" and "Singapore Polytechnic" too.

Please do not suggest me a regular expression like this one below:

\n[A-Z][A-Za-z ]+

This is because this expression extract "Daniel Rossey" just because it appears on the 3rd line, this cannot work for me as the name will not always belong in the 3rd line for my case.

I want to form a regex that:

How do I do that?

Upvotes: 1

Views: 87

Answers (1)

anubhava
anubhava

Reputation: 785196

You can try this regex for the stated problem:

^(?!.*\b(Polytechnic|University)\b) *[A-Za-z][A-Za-z.]*(?: +[A-Za-z.]+){1,3} *$

RegEx Demo

(?!.*\b(Polytechnic|University)\b) is negative lookahead to fail the match if these keywords appear anywhere in the input line. Rest is for matching 2-4 words.

Upvotes: 1

Related Questions