简体   繁体   中英

Regular Expression to get LastName / FirstName from a text

I've a text file like :

67890                    LastName, FirstName                                                                          
09/10/2020 18:18:01                               Page 1 of 1
DOB    01/01/1982     38 years         Male
..........

I would to extract LastName, First Name separately, then with 2 regexp to grab:

  • LastName as all the Chars before the first comma "," removing spaces before it.
  • FirstName as all the chars after the first comma "," removing spaces after it.

I did a test for the LastName with (C#):

^.*?(?=,)

but is grabbing all before the comma , not just the LastName.

Also the Code :

(?<=^.{20})([^,])+

is grabbing 20 chars before the comma but includes the Spaces. I Would just grab all chars before comma, could also be 20 chars before comma not to include the initial numbers.

I'm using the Test Site :

http://regexstorm.net/tester

Many Thanks for help!

Last name extraction regex :

(?<=^[^,]*?\s+)\S+(?=,)

First name extraction regex :

(?<=,\s*)\S+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM