Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can anyone tell me how to extract the name from the below detail?

Singapore Polytechnic 

Daniel Rossey
Manager

66556652
test@gmail.com


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:
JavaScript
\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:

- Match names that have 2 up to 4 words
- Exclude words like "Polytechnic" or "University"
Posted
Updated 3-Feb-16 20:36pm
v2

1 solution

The best solution is: "don't use a regex".
Regular expressions are a text processing system, but they aren't good at any form of syntactic analysis - which is effectively what you want.
Instead, do it in code: break it into lines, use a basic regex to check each line is two words, then apply an exclusion filter in your code to discard lines with contain "Manager", "Polytechnic", or "University". While it is technically possible to do this in a regex, it would be horribly complex, difficult to understand and next to impossible to maintain. So when you discover that you need to add "College" to the exclusion list, the effort you need to put is becomes horrible. Adding words to your exclusion list is trivial, and needn't even require any code changes!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900