Regex for the beginning of the line - or - last number in the sentence

new Regex(@"^[a-zA-Z]+\b +\b[a-zA-Z]?\b +\b[a-zA-Z]+$")

this matches

John Smith John B Goode

I am trying to modify this regex for the following cases:

some text before 12359 (John B? Goode) 10249?

that is sometimes the name comes after the a number at the end of the string and optionally before a final number at the end.

I have tried

new Regex(@"^|[0-9]+([a-zA-Z]+\b +\b[a-zA-Z]?\b +\b[a-zA-Z]+) *[0-9]*?$")

but that does not work because

  1. the ^|[0-9]+ only matches numbers anymore and not beginning of line
  2. the group is always an empty string that match something like sometext 12354 (the first number needs to not be at the end of a line.


This is all water under the bridge because I found more names at the end of the lines of data so this will not work.

However the solution to my problem was not throwing the OR in a group.

You need parentheses around the alternation:


Your expression is equivalent to this:

new Regex(@"^|()")

It always matches the start of the string and nothing else.