Fnkraf
Fnkraf

Reputation: 103

Make Regex accept hyphen/dash in the middle of required string

I need to create a logfile using powershell. It needs to have the patients first name, last name and birthdate. I am currently trying to get the last name to work..

I have a regex that correctly outputs the patients last name, but there are some cases in which the patient has a last name with a dash in the middle. EXAMPLE: Jonas Bauer-Schönemauer

My current Regex only matches "Bauer", but it's suppposed to match the whole lastname. This is my issue.

Below is my current Regex line. The first match group is for a random string of numbers and "3101", which is the last names prefix in this medical file.

^(\d+3101)(\p{L}+)

Here is an excerpt of the file im trying to do this out of (I put the numbers at the beginning of each line, those arent in the file!):

1      01380006310
2      014810000722
3      01092063
4      014921802.10
5      0220102GE Healthcare
6      0190103CardioSoft
7      0140132V6.73
8      01630000085271
9      0253101Bauer-Schönemauer
10     0143102Jonas
11     017310321051937

Line 9: Last name

Line 10: First name

Line 11: Birthdate (0173103[21.05.1937])

Could anyone help me out?

TL;DR: I need the regex to also match last names with a hyphen in the middle.

Upvotes: 0

Views: 1341

Answers (1)

user1016274
user1016274

Reputation: 4209

You should us a character class [] :

^(\d+3101)([\p{L}-]+)

Using -match and extracting the second submatch:

PS D:\> "0253101Bauer-Schönemauer" -match "^(\d+3101)([\p{L}-]+)"  
True  
PS D:\> $Matches[2]  
Bauer-Schönemauer  

Upvotes: 1

Related Questions