Reputation: 723
My input string looks like this:
Employee: Joe Banana
Department: Sales
Location: New York
Bla bla bla
Note:
even more bla
This regex does the trick extracting the full name, i.e. Joe Banana
(?s).*(?-s)(?m)^\s*(Employee): .*\s(.*)$(?-m)(?s).*(?-s)(?m)^\s*(Note):$(?-m)(?s).*
However this doesn't work for first name, but extract Department instead:
(?s).*(?-s)(?m)^\s*(Employee): (.*)\s.*$(?-m)(?s).*(?-s)(?m)^\s*(Note):$(?-m)(?s).*
Why don't these regex extract the first name and the last name?
I need to keep searching for those groups as we need to stop searching after Note:
Upvotes: 0
Views: 1449
Reputation: 161
If you use this, the first capture group will be the first name and the second one will be the last name:
Employee:\s+(\w+)\s+(\w+)
Upvotes: 0
Reputation: 723
I came up with this
FIRSTNAME
(?s).*(?-s)(?m)^\s*(Employee): (.*?)\s.*$(?-m)(?s).*(?-s)(?m)^\s*(Note):$(?-m)(?s).*
LASTNAME
(?s).*(?-s)(?m)^\s*(Employee): .*?\s(.*)$(?-m)(?s).*(?-s)(?m)^\s*(Note):$(?-m)(?s).*
Upvotes: 0
Reputation: 43169
Here you go:
Employee:\s*(?P<employee>.+)[\n\r]
Department:\s*(?P<department>.+)
See a demo on regex101.com (and mind the different modifiers!)
Upvotes: 2
Reputation: 1227
Try this one, the first match group is the first name the second match group is the last name :
(?m)^Employee:\s+(w+)\s+(w+)
or replace (.*) by (w+)\s+(w+) in your regex :
(?s).(?-s)(?m)^\s(Employee): .\s(w+)\s+(w+)$(?-m)(?s).(?-s)(?m)^\s*(Note):$(?-m)(?s).*
Upvotes: 1