The regex prefix matches only if the look-behind view
I'm trying to write a regex to parse a (seemingly very simple) piece of text.
some stuff
First name: John
Last name: Smith
more stuff
I want to write down the first and last name, so I tried regex:
(?<=First name:\s*)(?<FirstName>\w+)(?<=\s*Last name:\s*)(?<LastName>\w+)
It cannot find a match. Each part (first and last name) works individually, but they don't work together. In addition, the following works
(?<=John\s*Last name:\s*)(?<LastName>\w+)
but when I move "John" out of the mismatch group ...
John(?<=\s*Last name:\s*)(?<LastName>\w+)
... it doesn't match!
What am I doing wrong here?
a source to share
Since look-behind assertions are zero-width (that is, they consume no characters), capturing FirstName will match / record anything after Name, in this case John. After this first match, the position on the target line will be immediately after "John". But since the next part of the regex is another look, the regex will look to see if what immediately precedes its current position matches your outer text, in this case "Last Name:". Since it is actually preceded by "John", the whole regex fails and doesn't even hit Smith.
a source to share